1
1
package com.localizationsettings
2
2
3
+ import android.content.Context
4
+ import android.content.SharedPreferences
5
+ import android.os.Build
6
+ import androidx.appcompat.app.AppCompatDelegate
7
+ import androidx.core.os.LocaleListCompat
8
+ import com.facebook.react.bridge.Promise
3
9
import com.facebook.react.bridge.ReactApplicationContext
4
10
import com.facebook.react.bridge.ReactMethod
5
- import com.facebook.react.bridge.Promise
11
+ import java.util.*
12
+
6
13
7
14
class LocalizationSettingsModule internal constructor(context : ReactApplicationContext ) :
8
15
LocalizationSettingsSpec (context) {
@@ -11,11 +18,99 @@ class LocalizationSettingsModule internal constructor(context: ReactApplicationC
11
18
return NAME
12
19
}
13
20
14
- // Example method
15
- // See https://reactnative.dev/docs/native-modules-android
21
+ /* *
22
+ * Get IETF BCP 47 (language-COUNTRY "pl-PL")
23
+ * if country is not available in locale, then use system defaults (even if it's not 100% correct, like "pl-US")
24
+ **/
25
+ private fun getLanguageTag (language : String ): String {
26
+ // if language have format language_COUNTRY, then return it
27
+ if (Locale (language).country != " " ) return Locale (language).toLanguageTag()
28
+ // fallback for system country
29
+ return " $language -${Locale .getDefault().country} "
30
+ }
31
+
32
+
33
+ /* *
34
+ * Get current language
35
+ * returns string in IETF BCP 47 (language-COUNTRY "pl-PL")
36
+ * If API version >= 33, use native per-app language feature
37
+ * else, fallback to SharedPreferences
38
+ **/
39
+ private fun getCurrentLanguage (): String? {
40
+ // If API version is >= 33, then use per-app language settings
41
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
42
+ val currentLocaleName = if (! AppCompatDelegate .getApplicationLocales().isEmpty) {
43
+ // get per-app language
44
+ AppCompatDelegate .getApplicationLocales()[0 ]?.toLanguageTag()
45
+ } else {
46
+ // Fallback to the default System Locale
47
+ Locale .getDefault().toLanguageTag()
48
+ }
49
+ return currentLocaleName
50
+ }
51
+ // if API is < 33, then use SharedPreferences with fallback to default System Locale
52
+ if (getPreferences().getString(" languageFrom" , null ) == Locale .getDefault().language) {
53
+ return getPreferences().getString(" language" , Locale .getDefault().toLanguageTag())
54
+ }
55
+ return Locale .getDefault().toLanguageTag()
56
+ }
57
+
58
+
59
+ /* *
60
+ * Set current language
61
+ * passed language can be in ISO 639-1 (language "pl")
62
+ * or IETF BCP 47 (language-COUNTRY "pl-PL")
63
+ * If API version >= 33, use native per-app language feature
64
+ * else, fallback to SharedPreferences
65
+ **/
66
+ private fun setCurrentLanguage (language : String ) {
67
+ // If API version is >= 33, then use per-app language settings
68
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
69
+ val localeList = LocaleListCompat .forLanguageTags(getLanguageTag(language))
70
+ AppCompatDelegate .setApplicationLocales(localeList)
71
+ } else {
72
+ // if API is < 33, then set SharedPreferences language
73
+ val editor = getEditor();
74
+ editor.putString(" languageFrom" , Locale .getDefault().language)
75
+ editor.putString(" language" , getLanguageTag(language))
76
+ editor.apply ()
77
+ }
78
+ }
79
+
80
+
81
+ /* *
82
+ * Expose functions to react-native
83
+ **/
16
84
@ReactMethod
17
- override fun multiply (a : Double , b : Double , promise : Promise ) {
18
- promise.resolve(a * b)
85
+ override fun getLanguage (promise : Promise ) {
86
+ promise.resolve(getCurrentLanguage())
87
+ }
88
+
89
+ @ReactMethod
90
+ override fun setLanguage (language : String ) {
91
+ setCurrentLanguage(language)
92
+ }
93
+
94
+ /* *
95
+ * Expose constants to react-native
96
+ **/
97
+ override fun getConstants (): MutableMap <String , String ?>? {
98
+ val constants: MutableMap <String , String ?> = HashMap ()
99
+ constants[" language" ] = getCurrentLanguage()
100
+ return constants
101
+ }
102
+
103
+ /* *
104
+ * SharedPreferences (only used when API version is below 33)
105
+ **/
106
+ private fun getPreferences (): SharedPreferences {
107
+ return reactApplicationContext.getSharedPreferences(
108
+ " LocalizationSettings" , Context .MODE_PRIVATE
109
+ )
110
+ }
111
+
112
+ private fun getEditor (): SharedPreferences .Editor {
113
+ return getPreferences().edit()
19
114
}
20
115
21
116
companion object {
0 commit comments