How best to implement a language change in the development of applications for Android, if it is any non-standard languages ​​(for example, dialects of Chinese or outdated Russian)? Such languages, in principle, are not logical to configure automatically, because I’m interested in two things about this issue:

  • How is it correct in this case (i.e. manual change of language without locale) to create a res-файл with an additional language? Surely not the same as usual .
  • What is the best way to activate this language through the application? It is clear that it will be necessary to create the corresponding item in the settings, but what principle should create a method that replaces the interface language?
  • one
    You can change the default locale. Locale locale = new Locale (lang); Locale.setDefault (locale); Configuration config = new Configuration (); config.locale = locale; res.updateConfiguration (config, res.getDisplayMetrics ()); It definitely works for Farsi, which is somehow very conditionally supported by android and cannot be selected as the language of the system. And for it, you can create resources with the fa quantifier. But I'm not sure that you can create resources with a very random quantifier and that it will work for an outdated Russian. - lllyct 1:16

1 answer 1

You can create your own locales. And work with them as standard, both in terms of resources and code.

For example, create a locale "ru_MY".

enter image description here

An example of switching locales is on krtinka. but better give it a code

 // set your locale try { Constructor<Locale> constructor = Locale.class.getConstructor(String.class, String.class); constructor.setAccessible(true); Locale locale = constructor.newInstance("ru", "MY"); // <-- you locale (for example ru_MY) Locale.setDefault(locale); Configuration config = getBaseContext().getResources().getConfiguration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); } catch (Throwable e) { e.printStackTrace(); } 

PS don't forget that the dialect and the country are delimited by the letter "r" values-ru-rMY

  • Thank you for the answer! He was gone for a very long time. - Bokov Gleb