Skip to content

Commit fb750b9

Browse files
committed
Add LanguageHelper & language picker
1 parent 0dd94fc commit fb750b9

4 files changed

Lines changed: 77 additions & 27 deletions

File tree

app/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
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)
55
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)
66
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.
7-
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)
8-
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.
9-
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)
7+
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)
8+
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)
9+
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.
10+
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)

app/src/main/java/com/asemlab/samples/ui/MainActivity.kt

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.asemlab.samples.ui
22

33
import android.Manifest
4-
import android.app.LocaleConfig
5-
import android.app.LocaleManager
64
import android.content.pm.PackageManager
75
import android.os.Build
86
import android.os.Bundle
9-
import android.os.LocaleList
107
import android.view.View
118
import androidx.annotation.RequiresApi
129
import androidx.appcompat.app.AppCompatActivity
@@ -39,11 +36,6 @@ class MainActivity : AppCompatActivity() {
3936
true
4037
)
4138

42-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
43-
// TODO Set per-app supported languages dynamically
44-
updateSupportedLocales()
45-
}
46-
4739
}
4840

4941
fun selectFragment(view: View) {
@@ -61,20 +53,4 @@ class MainActivity : AppCompatActivity() {
6153
fragmentTransaction.commit()
6254
}
6355

64-
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
65-
fun updateSupportedLocales() {
66-
val localeManager = applicationContext
67-
.getSystemService(LocaleManager::class.java)
68-
69-
// For getOverrideLocaleConfig
70-
val overrideLocaleConfig = localeManager.overrideLocaleConfig
71-
val supportedLocales = overrideLocaleConfig?.supportedLocales
72-
73-
// For setOverrideLocaleConfig, update supported and remove old ones
74-
localeManager.overrideLocaleConfig = LocaleConfig(
75-
LocaleList.forLanguageTags("en-US,ja-JP,ar")
76-
)
77-
78-
}
79-
8056
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.asemlab.samples.utils
2+
3+
import android.app.LocaleConfig
4+
import android.app.LocaleManager
5+
import android.content.Context
6+
import android.os.Build
7+
import android.os.LocaleList
8+
import androidx.annotation.RequiresApi
9+
import androidx.appcompat.app.AlertDialog
10+
import androidx.appcompat.app.AppCompatDelegate
11+
import androidx.core.os.LocaleListCompat
12+
import com.asemlab.samples.R
13+
14+
object LanguageHelper {
15+
16+
// TODO Set per-app supported languages dynamically
17+
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
18+
fun updateSupportedLocales(context: Context) {
19+
val localeManager = context
20+
.getSystemService(LocaleManager::class.java)
21+
22+
// For getOverrideLocaleConfig
23+
val overrideLocaleConfig = localeManager.overrideLocaleConfig
24+
val supportedLocales = overrideLocaleConfig?.supportedLocales
25+
26+
// For setOverrideLocaleConfig, update supported and remove old ones
27+
localeManager.overrideLocaleConfig = LocaleConfig(
28+
LocaleList.forLanguageTags("en-US,ar")
29+
)
30+
31+
}
32+
33+
34+
// TODO Use AndroidX to change locale
35+
fun showPickerDialog(context: Context) {
36+
var selectedLocale = AppCompatDelegate.getApplicationLocales()[0]?.language ?: "en"
37+
val selectedItem =
38+
context.resources.getStringArray(R.array.language_codes).indexOf(selectedLocale)
39+
40+
AlertDialog.Builder(context)
41+
.setTitle("Choose your language")
42+
.setSingleChoiceItems(R.array.languages, selectedItem) { _, which ->
43+
when (which) {
44+
0 -> selectedLocale = "en"
45+
1 -> selectedLocale = "ar"
46+
2 -> selectedLocale = "de"
47+
}
48+
}
49+
.setNegativeButton("Close") { d, _ ->
50+
d.dismiss()
51+
}
52+
.setPositiveButton("Apply") { d, _ ->
53+
54+
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(selectedLocale)
55+
// Call this on the main thread as it may require Activity.restart()
56+
AppCompatDelegate.setApplicationLocales(appLocale)
57+
58+
d.dismiss()
59+
}
60+
.show()
61+
}
62+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<resources>
22
<string name="app_name" translatable="false">ContactsContent</string>
33
<string name="language_title">English</string>
4+
<string-array name="languages" translatable="false">
5+
<item>English</item>
6+
<item>العربية</item>
7+
<item>Deutsch</item>
8+
</string-array>
9+
10+
<string-array name="language_codes" translatable="false">
11+
<item>en</item>
12+
<item>ar</item>
13+
<item>de</item>
14+
</string-array>
415
</resources>

0 commit comments

Comments
 (0)