Skip to content

Commit

Permalink
Add LanguageHelper & language picker
Browse files Browse the repository at this point in the history
  • Loading branch information
AsemLab committed Oct 6, 2024
1 parent 0dd94fc commit fb750b9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 27 deletions.
7 changes: 4 additions & 3 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
2. [DownloadManager](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/download/FileDownloaderImp.kt): is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. [Official](https://developer.android.com/reference/android/app/DownloadManager)
3. [Epoxy](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/epoxy/EpoxyActivity.kt): is an Android library for building complex screens in a RecyclerView. [Official](https://github.com/airbnb/epoxy)
4. [Pass data between components](https://github.com/AsemLab/Samples/tree/main/app/src/main/java/com/asemlab/samples/passdata): demonstrate how to pass data from Activity to Activity/Fragment and from Fragment to Activity/Fragment.
5. [Photo Picker](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): provides a browsable interface that presents the user with their media library, sorted by date from newest to oldest. [Official](https://developer.android.com/training/data-storage/shared/photopicker)
6. [Progress in Notifications](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/NotificationProgressActivity.kt): display and update progressBar in notification.
7. [RegisterForActivityResult](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): call an activity and retrieve the result from it. [Official](https://developer.android.com/training/basics/intents/result)
5. [Per-app language preferences](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/utils/LanguageHelper.kt): allow users to set their preferred language for each app. The app specific preferred language will override the system language for that particular application. [Official](https://developer.android.com/guide/topics/resources/app-languages)
6. [Photo Picker](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): provides a browsable interface that presents the user with their media library, sorted by date from newest to oldest. [Official](https://developer.android.com/training/data-storage/shared/photopicker)
7. [Progress in Notifications](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/NotificationProgressActivity.kt): display and update progressBar in notification.
8. [RegisterForActivityResult](https://github.com/AsemLab/Samples/blob/main/app/src/main/java/com/asemlab/samples/ui/RegisterForResultActivity.kt): call an activity and retrieve the result from it. [Official](https://developer.android.com/training/basics/intents/result)
24 changes: 0 additions & 24 deletions app/src/main/java/com/asemlab/samples/ui/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.asemlab.samples.ui

import android.Manifest
import android.app.LocaleConfig
import android.app.LocaleManager
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.view.View
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -39,11 +36,6 @@ class MainActivity : AppCompatActivity() {
true
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// TODO Set per-app supported languages dynamically
updateSupportedLocales()
}

}

fun selectFragment(view: View) {
Expand All @@ -61,20 +53,4 @@ class MainActivity : AppCompatActivity() {
fragmentTransaction.commit()
}

@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
fun updateSupportedLocales() {
val localeManager = applicationContext
.getSystemService(LocaleManager::class.java)

// For getOverrideLocaleConfig
val overrideLocaleConfig = localeManager.overrideLocaleConfig
val supportedLocales = overrideLocaleConfig?.supportedLocales

// For setOverrideLocaleConfig, update supported and remove old ones
localeManager.overrideLocaleConfig = LocaleConfig(
LocaleList.forLanguageTags("en-US,ja-JP,ar")
)

}

}
62 changes: 62 additions & 0 deletions app/src/main/java/com/asemlab/samples/utils/LanguageHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.asemlab.samples.utils

import android.app.LocaleConfig
import android.app.LocaleManager
import android.content.Context
import android.os.Build
import android.os.LocaleList
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import com.asemlab.samples.R

object LanguageHelper {

// TODO Set per-app supported languages dynamically
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
fun updateSupportedLocales(context: Context) {
val localeManager = context
.getSystemService(LocaleManager::class.java)

// For getOverrideLocaleConfig
val overrideLocaleConfig = localeManager.overrideLocaleConfig
val supportedLocales = overrideLocaleConfig?.supportedLocales

// For setOverrideLocaleConfig, update supported and remove old ones
localeManager.overrideLocaleConfig = LocaleConfig(
LocaleList.forLanguageTags("en-US,ar")
)

}


// TODO Use AndroidX to change locale
fun showPickerDialog(context: Context) {
var selectedLocale = AppCompatDelegate.getApplicationLocales()[0]?.language ?: "en"
val selectedItem =
context.resources.getStringArray(R.array.language_codes).indexOf(selectedLocale)

AlertDialog.Builder(context)
.setTitle("Choose your language")
.setSingleChoiceItems(R.array.languages, selectedItem) { _, which ->
when (which) {
0 -> selectedLocale = "en"
1 -> selectedLocale = "ar"
2 -> selectedLocale = "de"
}
}
.setNegativeButton("Close") { d, _ ->
d.dismiss()
}
.setPositiveButton("Apply") { d, _ ->

val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(selectedLocale)
// Call this on the main thread as it may require Activity.restart()
AppCompatDelegate.setApplicationLocales(appLocale)

d.dismiss()
}
.show()
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<resources>
<string name="app_name" translatable="false">ContactsContent</string>
<string name="language_title">English</string>
<string-array name="languages" translatable="false">
<item>English</item>
<item>العربية</item>
<item>Deutsch</item>
</string-array>

<string-array name="language_codes" translatable="false">
<item>en</item>
<item>ar</item>
<item>de</item>
</string-array>
</resources>

0 comments on commit fb750b9

Please sign in to comment.