Android localize es-r419

前端 未结 4 1299
情歌与酒
情歌与酒 2021-02-01 21:51

I\'m localizing my app and one of the language/region supported is Espanol-419. Android doesn\'t support the naming convention values-es-r419 but it does accept values-en-rGB.

4条回答
  •  春和景丽
    2021-02-01 22:46

    419 comes from here, and is for any LatinoAmérica (Spanish) regions.

    Español (Colombia), Español (México), etc, will use values-es-r419

    We can also use this for each region instead of names, although it is more difficult

    values-en-r840 intead of values-en-rUS

    But, if we want a language for each region, the file must have the language followed by "-r" and the country code. The name is added automatically when choosing the region.

    To do that, create a "value resource file", choose the "Locale" option and choose the region.

    If the device language is "English (United States)" will take values-en-rUS/strings.xml.

    If the device has a language with a country that we do not support, will use values-en/strings.xml (is used by any region).

    Now, if you want to programmatically add the language, you cannot set the language as "en_US" or "en-rUS" because it doesn't exists, "en_US" is still "en". That's why you need to add the country to the Locale language (android supports upper or lower case)

    //Example language = "en" country = "US"
    private void setLanguage(Context context, String language, String country) {
        Configuration config = new Configuration(context.getResources().getConfiguration());
        config.setLocale(new Locale(language, country));
    
        //Copies the fields from delta into this Configuration object, keeping track of which ones have changed.
        config.updateFrom(new Resources(context.getResources().getAssets(),
                context.getResources().getDisplayMetrics(), config).getConfiguration());
    
        context.createConfigurationContext(config);
    }
    

提交回复
热议问题