Skip to content

cloud273/localization_android

Repository files navigation

Localization

What's this?

Localization is a library for multi-language application written in Kotlin.

Requirements

  • Android 5.0 (minSDK 21)

Installation

MavenCentral

implementation 'com.cloud273:cllocalization:1.0.0'

Usage

Required files:

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"
}

Initialize:

  • 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 language:

  • 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()
}

Code:

  • 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"

Example

Please find in project

Contribution

You are welcome to fork and submit pull requests.

License

Localization is open-sourced software, licensed under the MIT license.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages