Search code examples
androidlocalizationtoast

How make localization for toasts?


I'm wrote localization for ui texts using

String languageToLoad  = "en_EN";
 Locale locale = new Locale(languageToLoad); 
 Locale.setDefault(locale);
 Configuration config = new Configuration();
 config.locale = locale;
 context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());

This good working for UI views, but my toasts don't have this. How fix it? Strings was created in strings.xml with Locale qualifier.


Solution

  • This is quite easy:

    String string = getString(R.string.your_string_name);
    

    If the string is saved in the right language folder it will be localised automatically.

    Read more here: https://developer.android.com/guide/topics/resources/string-resource#:~:text=android%3Atext%3D%22%40string%2Fhello%22%20%2F%3E&text=String%20string%20%3D%20getString%20(R.,int)%20to%20retrieve%20a%20string.