Skip to content

Commit 0dd94fc

Browse files
committed
Implement per-app language preferences (Android 13+)
1 parent ada8534 commit 0dd94fc

File tree

9 files changed

+70
-8
lines changed

9 files changed

+70
-8
lines changed

app/build.gradle.kts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ android {
3939

4040
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
4141
// TODO Set general name for outputs, for .aab files
42-
setProperty("archivesBaseName", "${Configuration.versionName}(${Configuration.versionCode})")
42+
setProperty(
43+
"archivesBaseName",
44+
"${Configuration.versionName}(${Configuration.versionCode})"
45+
)
46+
47+
// TODO Include language resources in the APK for these specified languages (Optional)
48+
resourceConfigurations += mutableSetOf("en", "ar", "de")
4349
}
4450

4551
buildTypes {
@@ -62,6 +68,10 @@ android {
6268
buildFeatures {
6369
dataBinding = true
6470
}
71+
// TODO Generate per-app language support automatically, (NOT RECOMMENDED)
72+
// androidResources {
73+
// generateLocaleConfig = true
74+
// }
6575
}
6676

6777
dependencies {

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
65
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
76
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
87
<uses-permission android:name="android.permission.READ_CONTACTS" />
98
<uses-permission android:name="android.permission.INTERNET" />
109

10+
<!-- TODO Add locales_config.xml -->
1111
<application
1212
android:allowBackup="true"
1313
android:icon="@mipmap/ic_launcher"
1414
android:label="@string/app_name"
15+
android:localeConfig="@xml/locales_config"
1516
android:roundIcon="@mipmap/ic_launcher_round"
1617
android:supportsRtl="true"
17-
android:theme="@style/Theme.ContactsContent"
18-
tools:targetApi="31">
18+
android:theme="@style/Theme.ContactsContent">
1919
<activity
2020
android:name=".ui.BiometricActivity"
2121
android:exported="true" />

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

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

33
import android.Manifest
4+
import android.app.LocaleConfig
5+
import android.app.LocaleManager
46
import android.content.pm.PackageManager
57
import android.os.Build
68
import android.os.Bundle
9+
import android.os.LocaleList
710
import android.view.View
811
import androidx.annotation.RequiresApi
912
import androidx.appcompat.app.AppCompatActivity
@@ -36,6 +39,11 @@ class MainActivity : AppCompatActivity() {
3639
true
3740
)
3841

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

4149
fun selectFragment(view: View) {
@@ -53,5 +61,20 @@ class MainActivity : AppCompatActivity() {
5361
fragmentTransaction.commit()
5462
}
5563

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+
}
5679

5780
}

app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
android:layout_height="match_parent"
88
tools:context="com.asemlab.samples.ui.MainActivity">
99

10+
<TextView
11+
android:id="@+id/titleTV"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="@string/language_title"
15+
android:textAppearance="@style/TextAppearance.AppCompat.Title"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
1020
<Button
1121
android:id="@+id/frag1"
1222
android:layout_width="wrap_content"
@@ -15,7 +25,7 @@
1525
android:text="Frag 1"
1626
app:layout_constraintEnd_toStartOf="@+id/frag2"
1727
app:layout_constraintStart_toStartOf="parent"
18-
app:layout_constraintTop_toTopOf="parent" />
28+
app:layout_constraintTop_toBottomOf="@+id/titleTV" />
1929

2030
<Button
2131
android:id="@+id/frag2"
@@ -25,7 +35,7 @@
2535
android:text="Frag 2"
2636
app:layout_constraintEnd_toEndOf="parent"
2737
app:layout_constraintStart_toEndOf="@+id/frag1"
28-
app:layout_constraintTop_toTopOf="parent" />
38+
app:layout_constraintTop_toBottomOf="@+id/titleTV" />
2939

3040
<androidx.fragment.app.FragmentContainerView
3141
android:id="@+id/fragment_section"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TODO Set Default Locale, that values folder has no postfix
2+
unqualifiedResLocale=en
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="language_title">العربية</string>
4+
</resources>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="language_title">Deutsch</string>
4+
</resources>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
2-
<string name="app_name">ContactsContent</string>
2+
<string name="app_name" translatable="false">ContactsContent</string>
3+
<string name="language_title">English</string>
34
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
3+
<!-- TODO Add supported languages -->
4+
<locale android:name="en" />
5+
<locale android:name="ar" />
6+
<locale android:name="ar-JO" />
7+
<locale android:name="de" />
8+
</locale-config>

0 commit comments

Comments
 (0)