Locale locale = new Locale("en_US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext().getResources().updateConfiguration(config, null);
String languageToLoad = "en"; // your language Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Set correct language (default or chosen) Locale locale = new Locale(getState()); Locale.setDefault(locale); newConfig.locale = locale; getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics()); }
package com.stackoverflow.q2357315; import java.util.Locale; public class Test { public static void main(String[] args) throws Exception { Locale.setDefault(new Locale("lt")); String s = "\u00cc"; System.out.println(s + " (" + s.length() + ")"); // Ì (1) s = s.toLowerCase(); System.out.println(s + " (" + s.length() + ")"); // i̇̀ (3) } }
throws MissingResourceException Locale locale = Locale.getDefault(); return (ListResourceBundle)ResourceBundle.getBundle(className, locale); return (ListResourceBundle)ResourceBundle.getBundle( className, new Locale("en", "US")); throw new MissingResourceException( "Could not load any resource bundles." + className, className, "");
@Test public void dateOtherLocale() { Locale defaultLocale = Locale.getDefault(); try { Locale.setDefault(new Locale("nl", "nl")); Calendar calendar = new GregorianCalendar(2008, 11, 18, 11, 20); calendar.setTimeZone(TimeZone.getTimeZone("CET")); long date = calendar.getTimeInMillis(); headers.setDate(date); assertEquals("Invalid Date header", "Thu, 18 Dec 2008 10:20:00 GMT", headers.getFirst("date")); assertEquals("Invalid Date header", date, headers.getDate()); } finally { Locale.setDefault(defaultLocale); } }
public static Locale getPreferredLocale(Context context) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); String languageCode = sp.getString(context.getString(R.string.content_language_key), context.getString(R.string.default_language_value)); try { if (languageCode.length() == 2) { return new Locale(languageCode); } else if (languageCode.contains("_")) { String country = languageCode.substring(languageCode.indexOf("_"), languageCode.length()); return new Locale(languageCode.substring(0, 2), country); } } catch (Exception ignored) { } return Locale.getDefault(); }
/** * Gets the ResourceBundle AKA language package. * Used for choosing the language of the UI. * * @return Returns the default resource bundle using the language specified in the config file. */ public static ResourceBundle getResourceBundle(String langSelect) { if (langSelect == null) { if (!getConfigString("lang", "").equals("")) { String[] langCode = getConfigString("lang", "").split("_"); LOGGER.info("Setting locale to " + getConfigString("lang", "")); return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control()); } } else { String[] langCode = langSelect.split("_"); LOGGER.info("Setting locale to " + langSelect); return ResourceBundle.getBundle("LabelsBundle", new Locale(langCode[0], langCode[1]), new UTF8Control()); } try { LOGGER.info("Setting locale to default"); return ResourceBundle.getBundle("LabelsBundle", Locale.getDefault(), new UTF8Control()); } catch (MissingResourceException e) { LOGGER.info("Setting locale to root"); return ResourceBundle.getBundle("LabelsBundle", Locale.ROOT); } }
/** * Method init * * @param languageCode * @param countryCode */ public static synchronized void init(String languageCode, String countryCode) { if (alreadyInitialized) { return; } I18n.resourceBundle = ResourceBundle.getBundle( Constants.exceptionMessagesResourceBundleBase, new Locale(languageCode, countryCode) ); alreadyInitialized = true; }
public class MyApplication extends Application { @Override public void onCreate() { updateLanguage(this); super.onCreate(); } public static void updateLanguage(Context ctx) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx); String lang = prefs.getString("locale_override", ""); updateLanguage(ctx, lang); } public static void updateLanguage(Context ctx, String lang) { Configuration cfg = new Configuration(); if (!TextUtils.isEmpty(lang)) cfg.locale = new Locale(lang); else cfg.locale = Locale.getDefault(); ctx.getResources().updateConfiguration(cfg, null); } }
return (XResourceBundle) ResourceBundle.getBundle(resourceName, locale); return (XResourceBundle) ResourceBundle.getBundle( XSLT_RESOURCE, new Locale("en", "US")); throw new MissingResourceException( "Could not load any resource bundles.", className, "");
import java.util.Locale; public class ToLocaleTest { public static void main(String[] args) throws Exception { Locale.setDefault(new Locale("lt")); //setting Lithuanian as locale String str = "\u00cc"; System.out.println("Before case conversion is "+str+ " and length is "+str.length());// Ì String lowerCaseStr = str.toLowerCase(); System.out.println("Lower case is "+lowerCaseStr+ " and length is "+lowerCaseStr.length());// iı` } }
Locale locale = Locale.getDefault(); String suffix = getResourceSuffix(locale); return (XSLTErrorResources) ResourceBundle.getBundle(className + suffix, locale); return (XSLTErrorResources) ResourceBundle.getBundle(className, new Locale("en", "US")); throw new MissingResourceException( "Could not load any resource bundles.", className, "");
@Test public void testSingleBandedCoverage_GEOS7311() throws Exception { Locale defaultLocale = Locale.getDefault(); Locale.setDefault(new Locale("es", "ES")); testSingleBandedCoverage(); Locale.setDefault(new Locale("fr", "FR")); testSingleBandedCoverage(); Locale.setDefault(defaultLocale); }
public CustomDateFormatExtractor(String timePattern, String localeString) { Locale locale = (localeString != null)? new Locale(localeString): Locale.getDefault(); this.timePattern = timePattern; builder = new FormatterBuilder(); builder.locale = locale; parsePatternTo(builder, timePattern); textPattern = builder.toTextPattern(); }
import android.app.Activity; import android.content.res.Configuration; import android.os.Bundle; public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String languageToLoad = "fa"; // your language Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); this.setContentView(R.layout.main); } }
/** * Gets the resource bundle for the specified language. * @param languageCode lowercase two-letter ISO-639 code. * @return the resource bundle for the specified language. */ public static ResourceBundle getMessages(String languageCode) { if (languageCode.length() > 3) { throw new RuntimeException("Use a character code (ISO-639 code), not a full language name: " + languageCode); } ResourceBundle messages = ResourceBundle.getBundle( JLanguageTool.MESSAGE_BUNDLE, new Locale(languageCode)); return messages; }
import java.util.Locale; public class Test { public static void main(String[] args) { Locale.setDefault(new Locale("hi", "IN")); System.out.printf("String: %s; Number: %d\n", 1234, 1234); } }