Java tutorial
//package com.java2s; //License from project: Open Source License import android.text.TextUtils; import java.util.Locale; public class Main { /** * Length of a {@link String} (representing a language code) when there is no region included. * For example: "en" contains no region, "en_US" contains a region (US) * * Used to parse a language code {@link String} when creating a {@link Locale}. */ private static final int NO_REGION_LANG_CODE_LEN = 2; /** * Index of a language code {@link String} where the region code begins. The language code * format is cc_rr, where cc is the country code (e.g. en, es, az) and rr is the region code * (e.g. us, au, gb). */ private static final int REGION_SUBSTRING_INDEX = 3; /** * Gets a locale for the given language code. */ public static Locale languageLocale(String languageCode) { if (TextUtils.isEmpty(languageCode)) return Locale.getDefault(); if (languageCode.length() > NO_REGION_LANG_CODE_LEN) { return new Locale(languageCode.substring(0, NO_REGION_LANG_CODE_LEN), languageCode.substring(REGION_SUBSTRING_INDEX)); } return new Locale(languageCode); } }