Localization
is a library for multi-language application written in Kotlin.
- Android 5.0 (minSDK 21)
implementation 'com.cloud273:cllocalization:1.0.0'
Add files to res/raw:
-
English: 'localization_en.json'
-
Vietnamese: 'localization_vi.json'
-
And more ....
Content of file:
{
"button": "My button",
"label": "My label",
"cancel": "Cancel",
"next": "Next",
"hint": "Tap here to input"
}
- Setup main application and receiver in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cloud273.localization.example">
<!--Setup application to your class: android:name=".MyApplication"-->
<application
android:name=".MyApplication">
<!--Setup receiver -->
<receiver
android:name="com.cloud273.localization.LocaleChangedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
- Initialize list of supported language inside your main application (MyApplication):
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
CLLocalization.initialize(this, listOf("en", "vi"))
}
}
Note: Device language will be used
- Change to target language:
CLLocalization.setLanguage("en")
- Change back to use device language:
CLLocalization.setLanguage(null)
- Monitor language change in activity:
override fun onDestroy() {
super.onDestroy()
stopMonitorLanguageChanged()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startMonitorLanguageChanged()
}
- String
val localizedString = "text".localized
- Activity:
activity.lTitle = "title"
- Fragment:
fragment.lTitle = "title"
- TextView:
textView.lText = "label"
- Button:
button.lText = "button"
- RadioButton:
button.lText = "button"
- CheckBox:
checkBox.lText = "title"
- EditText:
editText.hint = "hint"
Please find in project
You are welcome to fork and submit pull requests.
Localization
is open-sourced software, licensed under the MIT
license.