Android How to use the same localized messages for all languages ​​in the country?

My country (Spain) has several languages ​​(es-ES, ca-ES, gl-ES, eu-ES). At the moment, we will not add all languages, so we would like to use the main language in Spain, that is, Spanish. We would like to display /values-es/strings.xml when the user has selected one of the other languages ​​in the country. How can we do this?

Oh, and we would like to use English as the default language ( /values/strings.xml ).

It would be great to have something like /values-es/strings.xml , but I believe that this cannot be done, because the first code should be the language code.

Now we copy the /values-es/strings.xml file to other folders ( values-ca , values-gl and values-eu ), but we would like to avoid this.

+7
android localization
source share
7 answers

I think you should only have 2 folders with 1 strings.xml for each other:

  • res / values ​​/strings.xml this resource will contain your English text;
  • res / values-es / strings.xml this resource will contain your Spanish text.

When your application is installed on a device configured in Italian, it will use the file resource in case 1.

When your application is installed on a device configured for Spanish (and there is a lot of Spanish there, think about the countries of South America ), it will use a file resource in case 2.

You can easily do this with Android Studio:

  • right click the res folder
  • go to Create> Android Resource Directory.
  • a window will show you some options; select Locale , and then click the button with these characters "β†’"
  • then in the Language list, select es: Spanish and click "OK" as shown in the image below (note that by default Only a specific region is selected Any region !)

Add Language Image Guide

From experience: I have never come across Breton, Corsican or Provencal users who claim to fully translate the application in their language (by default, the application has English both by default and French).

+1
source share

I would say that you want to do something like this.

 if (Locale.getDefault().getISO3Country().equals("ESP")) { Locale[] locales = Locale.getAvailableLocales(); for (Locale loc : locales) if (loc.getISO3Language().equals("SPA")) { Locale.setDefault(loc); break; } } 

Note. I am not sure if I received the ISO3 code and country codes correctly. And you will also need to do something for the (rare?) Situation where the es-ES locale is not available.

0
source share

If you are trying to redefine Catalan in Spanish, you should probably have this in the values-ca/strings.xml .

The way to do what you ask is to provide resources in the corresponding resource folder of the mobile country code, which takes precedence over the resources of the language region.

Suppose you have the following situation:

The application code calls R.string.text_a Two resource files are available: res/values-mcc404/strings.xml , which includes text_a in the default language of the application, in this case, in English. res/values-hi/strings.xml , which includes text_a in Hindi. The application runs on a device that has the following configuration: The SIM card is connected to a mobile network in India (MCC 404). The language is set to Hindi (hi). Android will load text_a from res/values-mcc404/strings.xml (in English), even if the device is configured for Hindi. This is due to the fact that in the process of selecting resources, Android will prefer a MCC match by language match.

MCC for Spain is 214.

(see Localization )

0
source share

I found another difficult solution: hard links . Despite the fact that it does not completely eliminate the whole problem, at least it protects you from the routine task of copying a file through several directories or making equal changes to all existing files with the risk of missing something.

But I have to admit that there are some reservations:

1) Some IDEs do not support working with hard links by default. Intellij IDEA and Android Studio will break your hard links if you do not disable the "safe recording" option in the settings.

2) Most version control systems also do not support hard links by default. Take git for example. This will break existing hard links after changing or merging changes.

I am currently using a batch file to restore hard links after they break into git.

0
source share

In general, in the values ​​folder there should be only one strings.xml file containing the corresponding data. If we explicitly specify different values ​​in the folder, for example values-ca, values-es, whenever changes are made to the Android device, it will search for a specific folder and take the value of the corresponding lines. If the requirement to preserve uniform text means that it is better to have only values ​​→ strings.xml only a file with the required data. But with this approach, multilingual apk not possible, i.e. For another country, a different language is expected, again there will be changes. Therefore, wherever we need a single language, you can use only one folder, and wherever multilingual is preferable, we can have several values ​​-s, values-ca. Hope that helps

0
source share

How to try installing it in java, instead of using strings.xml. Because it programmatically gives you more flexibility at runtime.

  Configuration config = new Configuration(getResources().getConfiguration()); Locale locale = Locale.getDefault(); String country = locale.getCountry(); String language = locale.getLanguage(); if (country.equalsIgnoreCase("ES") && (language.equalsIgnoreCase("ca") || language.equalsIgnoreCase("gl") || language.equalsIgnoreCase("eu"))) { locale = new Locale("es"); } config.setLocale(locale); 

And then you can just have one /values-es/strings.xml for all languages ​​of ES countries.

0
source share

You can follow this link.

http://www.androidhive.info/2014/07/android-building-multi-language-supported-app/


https://www.tutorialspoint.com/android/android_localization.htm


https://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/


For example, if the code loads a line called "R.string.title", Android will select the correct value for that line at runtime by loading the corresponding strings.xml file from the corresponding res / values ​​directory.

To have a multilingual Android app, you need to provide Android localized resource files.

If the locale is "en-US", Android will look for the value of "R.string.title" - When it manages to find a line in the resource file, it uses that value and stops the search. This means that the default language acts as a return option, and when a translation exists, it is used instead. It also means that you must either localize everything or nothing, since viewing partially translated applications is usually worse than applications that are not translated at all.

-one
source share

All Articles