Android examples for Internationalization:Locale
Creates a locale from a string specification.
/*//from w w w . j a va2 s. c o m * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ //package com.java2s; import java.util.HashMap; import java.util.Locale; import javax.annotation.Nonnull; public class Main { private static final HashMap<String, Locale> sLocaleCache = new HashMap<>(); /** * Creates a locale from a string specification. * @param localeString a string specification of a locale, in a format of "ll_cc_variant" where * "ll" is a language code, "cc" is a country code. */ @Nonnull public static Locale constructLocaleFromString( @Nonnull final String localeString) { synchronized (sLocaleCache) { if (sLocaleCache.containsKey(localeString)) { return sLocaleCache.get(localeString); } final String[] elements = localeString.split("_", 3); final Locale locale; if (elements.length == 1) { locale = new Locale(elements[0] /* language */); } else if (elements.length == 2) { locale = new Locale(elements[0] /* language */, elements[1] /* country */); } else { // localeParams.length == 3 locale = new Locale(elements[0] /* language */, elements[1] /* country */, elements[2] /* variant */); } sLocaleCache.put(localeString, locale); return locale; } } }