LocaleHub logoLocaleHub

Docs

Android SDK

Installation

Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}
And Install the dependency in your module build.gradle
dependencies {
    implementation 'com.github.locale-hub.sdk-android:core:<DESIRED_VERSION>'
    # If you use jetpack compose
    implementation 'com.github.locale-hub.sdk-android:androidview:<DESIRED_VERSION>'
    # If you use android views
    implementation 'com.github.locale-hub.sdk-android:composeview:<DESIRED_VERSION>'
}

Usage

You can look at demoapp for usage examples for both Compose and Android view.

Common setup

  • Import translations bundle into your app assets folder. You can download it from website Project > Commits.
  • Then create an Application that inherit ApplicationWithLocaleHub.
// No parameter will initialize SDK in Offline mode
// Adding the parameters in comment will initialize SDK in Remote mode (and fallback to Offline)
class MyDemoApplication : ApplicationWithLocaleHub(
//    projectId = "YOUR_PROJECT_ID",
//    apiKey = "YOUR_API_KEY",
//    deploymentTag = "YOUR_TAG",
//    sdkUrl = "http://192.168.0.1:3002/v1",
)

Compose View Minimal configuration

  • In any activity you want to enable LocaleHub
class ComposeViewActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContent {
      WithLocaleHub(this) {
        ComposableScreenDemo()
      }
    }

    // Your code here
  }
}
  • Then, in your composable components
Text(text = lhTranslate(key = "section.static.text"))

val value = "John"
Text(text = lhTranslate(key = "section.with_variables.text", variables = hashMapOf( "name" to value )))

Android View Minimal configuration

  • In any activity you want to enable LocaleHub, inherit from ActivityWithLocaleHubInterceptor
    • If ComponentActivity is not what you want, you can
      • Create a new BaseActivityClass inheriting from the Activity type you want (eg: AppCompatActivity)
      • Paste the content of ActivityWithLocaleHubInterceptor inside your BaseActivityClass
class AndroidViewActivity : ActivityWithLocaleHubInterceptor() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)

    // Your code here
  }
}
  • Translations in your views will be handled automatically by the interceptor based on @string/translationKey values.
  • In your code, you can translate as follow
val variableText = findViewById<TextView>(R.id.section_with_variables_content_string)
findViewById<EditText>(R.id.section_with_variables_content_input).addTextChangedListener {
  if (null != it) {
    variableText.text = lhTranslate(
      "section.with_variables.text",
      hashMapOf("name" to it.toString())
    )
  }
}