Localization for Canada defaults to UK; should default to US

后端 未结 2 1493
甜味超标
甜味超标 2021-01-14 15:52

I\'m working on an application that has localized strings for the UK, specified in res/values-en-rGB/strings.xml. My default string implementations, for the US,

2条回答
  •  Happy的楠姐
    2021-01-14 16:37

    1. Should happen automatically, without you doing anything. If you have values/strings.xml, values-en-rGB/strings.xml and values-en-rCA/strings.xml with the GB and CA versions containing only those strings that differ from the baseline (values/strings.xml) and if the user has his locale as en-CA, the system will load strings from values-en-rCA/strings.xml if not found, it'll fall back to values/strings.xml, the GB version wouldn't come in the picture.

    2. For this you'll have to locally change your app locale. Basically you get the current locale, if it is fr-CA, you manually set the locale to en-CA

      Locale enCALocale = new Locale("en", "CA"); 
      Locale.setDefault(enCALocale);
      Configuration config = new Configuration();
      config.locale = enCALocale;
      getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
      

    Update

    As Shobhit mentioned, starting Android N, you can now have a list of locales selected in the setting and you can decide their priority:

    In the above case the system will try and look for en-rCA/strings, if not found it'll look into en-rGB/strings instead of baseline, something that happened before N.

提交回复
热议问题