am bd855367: Merge "Don\'t assume languages are 2 letter codes."

* commit 'bd855367c41d588e9a3305d188707dde5b2c0a0a':
  Don't assume languages are 2 letter codes.
This commit is contained in:
Narayan Kamath
2014-07-31 12:16:38 +00:00
committed by Android Git Automerger

View File

@@ -190,33 +190,34 @@ public class SettingsHelper {
String localeString = loc.getLanguage(); String localeString = loc.getLanguage();
String country = loc.getCountry(); String country = loc.getCountry();
if (!TextUtils.isEmpty(country)) { if (!TextUtils.isEmpty(country)) {
localeString += "_" + country; localeString += "-" + country;
} }
return localeString.getBytes(); return localeString.getBytes();
} }
/** /**
* Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where * Sets the locale specified. Input data is the byte representation of a
* "ll" is the language code and "cc" is the country code. * BCP-47 language tag. For backwards compatibility, strings of the form
* {@code ll_CC} are also accepted, where {@code ll} is a two letter language
* code and {@code CC} is a two letter country code.
*
* @param data the locale string in bytes. * @param data the locale string in bytes.
*/ */
void setLocaleData(byte[] data, int size) { void setLocaleData(byte[] data, int size) {
// Check if locale was set by the user: // Check if locale was set by the user:
Configuration conf = mContext.getResources().getConfiguration(); Configuration conf = mContext.getResources().getConfiguration();
Locale loc = conf.locale;
// TODO: The following is not working as intended because the network is forcing a locale // TODO: The following is not working as intended because the network is forcing a locale
// change after registering. Need to find some other way to detect if the user manually // change after registering. Need to find some other way to detect if the user manually
// changed the locale // changed the locale
if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
final String[] availableLocales = mContext.getAssets().getLocales(); final String[] availableLocales = mContext.getAssets().getLocales();
String localeCode = new String(data, 0, size); // Replace "_" with "-" to deal with older backups.
String language = new String(data, 0, 2); String localeCode = new String(data, 0, size).replace('_', '-');
String country = size > 4 ? new String(data, 3, 2) : ""; Locale loc = null;
loc = null;
for (int i = 0; i < availableLocales.length; i++) { for (int i = 0; i < availableLocales.length; i++) {
if (availableLocales[i].equals(localeCode)) { if (availableLocales[i].equals(localeCode)) {
loc = new Locale(language, country); loc = Locale.forLanguageTag(localeCode);
break; break;
} }
} }