-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Skeptick/improve-localization
Improve localization
- Loading branch information
Showing
15 changed files
with
176 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
## Changing Localization | ||
|
||
As mentioned in the [README](../README.md#strings), the language to which the strings belong | ||
is indicated in the file name before the extension (e.g. `strings_en.xml`). | ||
|
||
During the application runtime, the localization selection is based on the system settings | ||
and changing the language should also be done at the application instance level. | ||
|
||
Examples of setting English language for all supported platforms: | ||
|
||
**Android & JVM** | ||
```kotlin | ||
Locale.setDefault(Locale("en")) | ||
``` | ||
> **Note** | ||
> For Android, don't forget to update the configuration as well. | ||
**iOS & MacOS** | ||
```swift | ||
UserDefaults.standard.set("en", forKey: "AppleLanguages") | ||
``` | ||
> **Note** | ||
> All used localizations must be declared in XCode project settings. | ||
**JS** | ||
|
||
In JS, there is no such possibility to change the language (without changing the language in the operating system or browser), | ||
so the best option is to use `LibresSettings`: | ||
|
||
```kotlin | ||
LibresSettings.languageCode = "en" | ||
``` | ||
|
||
This solution will also work for all other platforms but remember that this setting applies | ||
only to the selection of localization for strings in Libres, and some | ||
system components may remain untranslated. | ||
> **Warning** | ||
> The value in `LibresSettings.languageCode` has higher priority than system settings. | ||
## Plural rules | ||
|
||
In Android, the SDK solution is used to determine the plural form, | ||
in JVM 3rd-party library is used. But in Apple this API is closed and in JS it's completely absent, | ||
so to avoid burdening the library with heavy solutions, "out of the box" only rules are provided | ||
for [some languages](../libres-core/src/appleAndJsMain/kotlin/io/github/skeptick/libres/strings/PluralRules.kt). | ||
|
||
You can create a Pull Request with the required languages or define these values at runtime: | ||
|
||
```kotlin | ||
import io.github.skeptick.libres.strings.PluralRules | ||
import io.github.skeptick.libres.strings.PluralForm | ||
|
||
PluralRules["en"] = PluralRule { number -> | ||
when (number) { | ||
1 -> PluralForm.One | ||
else -> PluralForm.Other | ||
} | ||
} | ||
``` | ||
|
||
When defining rules you can refer to | ||
[this document](https://unicode-org.github.io/cldr-staging/charts/37/supplemental/language_plural_rules.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
libres-core/src/appleAndJsMain/kotlin/io/github/skeptick/libres/strings/PluralRules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.github.skeptick.libres.strings | ||
|
||
import io.github.skeptick.libres.strings.PluralForm.* | ||
|
||
fun interface PluralRule { | ||
fun getForm(number: Int): PluralForm | ||
} | ||
|
||
enum class PluralForm(internal val formName: String) { | ||
Zero("zero"), | ||
One("one"), | ||
Two("two"), | ||
Few("few"), | ||
Many("many"), | ||
Other("other") | ||
} | ||
|
||
object PluralRules { | ||
|
||
private val custom = mutableMapOf<String, PluralRule>() | ||
|
||
private val English = PluralRule { number -> | ||
when (number) { | ||
1 -> One | ||
else -> Other | ||
} | ||
} | ||
|
||
private val Russian = PluralRule { number -> | ||
when (number % 100) { | ||
11, 12, 13, 14 -> Many | ||
else -> when (number % 10) { | ||
1 -> One | ||
2, 3, 4 -> Few | ||
else -> Many | ||
} | ||
} | ||
} | ||
|
||
private val Ukrainian = PluralRule { number -> | ||
when (number % 100) { | ||
11, 12, 13, 14 -> Many | ||
else -> when (number % 10) { | ||
1 -> One | ||
2, 3, 4 -> Few | ||
else -> Many | ||
} | ||
} | ||
} | ||
|
||
private val Kazakh = PluralRule { number -> | ||
when (number) { | ||
1 -> One | ||
else -> Other | ||
} | ||
} | ||
|
||
private val French = PluralRule { number -> | ||
when (number) { | ||
0, 1 -> One | ||
else -> Other | ||
} | ||
} | ||
|
||
operator fun set(languageCode: String, value: PluralRule) { | ||
custom[languageCode] = value | ||
} | ||
|
||
operator fun get(languageCode: String): PluralRule { | ||
return when (languageCode) { | ||
"en" -> English | ||
"ru" -> Russian | ||
"uk" -> Ukrainian | ||
"kk" -> Kazakh | ||
"fr" -> French | ||
else -> custom[languageCode] ?: error("Plural rule for '$languageCode' not provided") | ||
} | ||
} | ||
|
||
operator fun plus(value: Pair<String, PluralRule>) { | ||
custom[value.first] = value.second | ||
} | ||
|
||
operator fun plusAssign(map: Map<String, PluralRule>) { | ||
custom += map | ||
} | ||
|
||
} |
64 changes: 0 additions & 64 deletions
64
libres-core/src/appleMain/kotlin/io/github/skeptick/libres/strings/PluralRules.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
libres-core/src/jsMain/kotlin/io/github/skeptick/libres/strings/PluralRules.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters