@@ -24,22 +24,27 @@ package com.hyperdevs.poeditor.gradle.utils
2424 * @return proper values file modifier (i.e. es-rMX)
2525 */
2626fun createValuesModifierFromLangCode (langCode : String ): String {
27- return if (! langCode.contains(" -" )) {
28- langCode
29- } else {
30- val langParts = langCode.split(" -" )
31- val language = langParts[0 ]
32- val region = langParts[1 ].toLowerCase()
27+ val langParts = langCode.split(" -" )
28+ val language = langParts[0 ]
29+ val region = langParts.getOrNull(1 )?.toLowerCase()
3330
34- return when (language) {
35- " zh" -> {
36- // Chinese support
37- handleChineseVariants(language, region)
31+ return when (language) {
32+ " zh" -> {
33+ // Chinese support
34+ handleChineseVariants(language, region).apply {
35+ logger.lifecycle(" INFO: Converted $langCode to $this " )
3836 }
39- else -> {
40- " $language -r${region.toUpperCase()} "
37+ }
38+
39+ " he" , " yi" , " id" -> {
40+ handleOldLocaleVariants(language, region).apply {
41+ logger.lifecycle(" INFO: Converted $langCode to $this " )
4142 }
4243 }
44+
45+ else -> {
46+ " $language${region?.let { " -r${it.toUpperCase()} " } ? : " " } "
47+ }
4348 }
4449}
4550
@@ -60,14 +65,48 @@ fun createValuesModifierFromLangCode(langCode: String): String {
6065 * - Chinese (traditional) will be set as values-b+zh+Hant.
6166 * - Regional Chinese variants will be set as values-zh-r<REGION>
6267 */
63- private fun handleChineseVariants (language : String , region : String ) = when (region) {
68+ private fun handleChineseVariants (language : String , region : String? ) = when (region) {
6469 " cn" -> {
6570 language
6671 }
72+
6773 " hans" , " hant" -> {
68- " b+$language + ${region.toLowerCase().capitalize()} "
74+ " b+$language${region.let { " + ${it. toLowerCase().capitalize()} " } } "
6975 }
76+
7077 else -> {
71- " $language -r${region.toUpperCase()} "
78+ " $language${region?.let { " -r${it.toUpperCase()} " } ? : " " } "
79+ }
80+ }
81+
82+ /* *
83+ * Handle locales that need translation to ISO-639 old locales, so Android devices can pick them up properly.
84+ *
85+ * According to [java.util.Locale] constructor docs in Android documentation:
86+ * ISO 639 is not a stable standard; some of the language codes it defines (specifically "iw", "ji", and "in")
87+ * have changed. This constructor accepts both the old codes ("iw", "ji", and "in") and the new codes
88+ * ("he", "yi", and "id"), but all other API on Locale will return only the OLD codes.
89+ *
90+ * We keep this in mind to convert new locale codes received from PoEditor to the old codes handled by Android.
91+ *
92+ * NOTE: this will most likely change if Android ever supports Java 17, where new codes are used for locales. There may
93+ * be a way to user the "-Djava.locale.useOldISOCodes=true" system property, though.
94+ *
95+ * References:
96+ * - Locale documentation: https://developer.android.com/reference/java/util/Locale.html#Locale(java.lang.String)
97+ * - List of supported languages up to Android 5.1: https://stackoverflow.com/a/7989085/9288365
98+ * - List of supported languages from Android 7 to Android 9: https://stackoverflow.com/a/52329560/9288365
99+ * - Android Issue Tracker issue regarding Indonesian locale: https://issuetracker.google.com/issues/36911507
100+ * - Android Issue Tracker issue regarding Hebrew locale: https://issuetracker.google.com/issues/36908826
101+ * - Java 17 locale changes: https://bugs.openjdk.org/browse/JDK-8267069
102+ */
103+ private fun handleOldLocaleVariants (language : String , region : String? ): String {
104+ val oldLanguageCode = when (language) {
105+ " he" -> " iw"
106+ " yi" -> " ji"
107+ " id" -> " in"
108+ else -> language
72109 }
110+
111+ return " $oldLanguageCode${region?.let { " -r${it.toUpperCase()} " } ? : " " } "
73112}
0 commit comments