Pages

Wednesday, January 15, 2025

How to add internationalization to your android application ?

 

Configure your project


First, you need to add the following configuration in your file build.gradle (Module : app) :


android {

.....

androidResources {
generateLocaleConfig = true
}

}

In the "res" directory, define the default language. To do that, you need to create resources.properties file with the following content  :

unqualifiedResLocale=en-US


You will probably need to resynchronize your project.


Define strings.xml for each country


You could create the string.xml file which will contains the translation

for the country you choose. For example, il you want to translate in Japanese,

create the strings.xml for Japanese :




You will see a strings.xml for the select country :




Define and use translation


In each strings.xml, you have to define variables with traduction .

Example: 


In the English strings.xml, you can set the value manually like this :
<resources>
    <string name="advanced">Advanced</string>
Then, in French strings.xml, you set the translation :
<resources>
    <string name="advanced">Avancé</string>


You can also define variables automatically (like advanced variable). To do that, put the cursor on a string you want to translate and do ALT - ENTER.

Then select Extract String resources :

Example :

In your code, you have the following line :
message: String = "Bienvenue !" 

Put the cursor on "Bienvenue" and ALT ENTER . You'll see this screen :





Click OK and Android Studio will generate the following automatically :

message: String = stringResource(R.string.welcome)


Other way to use locale in your code

If your phone is in Japanese, then this will use the corresponding strings.xml file
In my small Android application QuizzBee, I load files according to language :

var country = Locale.getDefault().getCountry()
if (country == "FR") {
...



Monday, January 13, 2025

Notes sur la mise à jour d'un projet Android : migration du sdk 33 vers sdk 34

 

La migration d'un projet Android de la version 33 vers la version 34 a été très simple.

J'ai tout d'abord procédé à la mise à jour des dependencies dans le fichier build.gradle.

Remarque :

  • J'avais mis la version 1.15 pour androidx.core mais cette version nécessite le sdk 35 au lieu du sdk 34, j'ai donc finalement opté pour le la version 1.13.1 :

// ================ CORE

def core_version = "1.13.1"

implementation "androidx.core:core-ktx:$core_version"


Ensuite, il suffit de vous rendre dans Project Structure et de modifier 

  • Target SDK 34
  • Compile SDK 34







Remarque :

Dans Android Studio, la ligne TargetSdk 34 dans  build.gradle était toujours avec une erreur dans l'IDE  (souligné en rouge). J'ai redémarré l'IDE et cela a disparu !


debugRuntimeClasspathCopy configuration has been deprecated for consumption

 

I saw warning during the gradle build.

To show details, I had the following in the gradle.properties file :

org.gradle.warning.mode=all


After that, I saw the error details in console :




To now, I didn't find the solution.That's why, I asked the question on StackOverFlow :

https://stackoverflow.com/questions/79351614/debugruntimeclasspathcopy-configuration-has-been-deprecated-for-consumption




How to add internationalization to your android application ?

  Configure your project First, you need to add the following configuration in your file build.gradle (Module : app) : android { ..... andr...