java.util.Locale.setDefault()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中java.util.Locale.setDefault()方法的一些代码示例,展示了Locale.setDefault()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Locale.setDefault()方法的具体详情如下:
包路径:java.util.Locale
类名称:Locale
方法名:setDefault

Locale.setDefault介绍

[英]Overrides the default locale. This does not affect system configuration, and attempts to override the system-provided default locale may themselves be overridden by actual changes to the system configuration. Code that calls this method is usually incorrect, and should be fixed by passing the appropriate locale to each locale-sensitive method that's called.
[中]覆盖默认区域设置。这不会影响系统配置,试图覆盖系统提供的默认区域设置本身可能会被系统配置的实际更改所覆盖。调用此方法的代码通常是不正确的,应该通过将适当的区域设置传递给调用的每个区域设置敏感方法来修复。

代码示例

代码示例来源:origin: stackoverflow.com

Locale locale = new Locale("en_US"); 
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);

代码示例来源:origin: stackoverflow.com

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());
}

代码示例来源:origin: apache/geode

@Override
protected void before() throws Throwable {
 originalLocale = Locale.getDefault();
 Locale.setDefault(initLocale);
 if (consumer != null)
  consumer.accept(initLocale);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
  }
}

代码示例来源:origin: stackoverflow.com

public class LocaleUtils {

  private static Locale sLocale;

  public static void setLocale(Locale locale) {
    sLocale = locale;
    if(sLocale != null) {
      Locale.setDefault(sLocale);
    }
  }

  public static void updateConfig(ContextThemeWrapper wrapper) {
    if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      Configuration configuration = new Configuration();
      configuration.setLocale(sLocale);
      wrapper.applyOverrideConfiguration(configuration);
    }
  }

  public static void updateConfig(Application app, Configuration configuration) {
    if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
      //Wrapping the configuration to avoid Activity endless loop
      Configuration config = new Configuration(configuration);
      config.locale = sLocale;
      Resources res = app.getBaseContext().getResources();
      res.updateConfiguration(config, res.getDisplayMetrics());
    }
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Override
  public void evaluate() throws Throwable {
    final Locale save = Locale.getDefault();
    try {
      Locale.setDefault(newLocale);
      stmt.evaluate();
    } finally {
      Locale.setDefault(save);
    }
  }
};

代码示例来源:origin: geoserver/geoserver

@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);
}

代码示例来源:origin: stackoverflow.com

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);
 }
}

代码示例来源:origin: stackoverflow.com

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);
 }
}

代码示例来源:origin: stackoverflow.com

private void restartInLocale(Locale locale)
{
  Locale.setDefault(locale);
  Configuration config = new Configuration();
  config.locale = locale;
  Resources resources = getResources();
  resources.updateConfiguration(config, resources.getDisplayMetrics());
  recreate();
}

代码示例来源:origin: hibernate/hibernate-orm

@BeforeClass
public static void beforeClass() {
  currentLocale = Locale.getDefault();
  
  // Turkish will generate a "dotless i" when toLowerCase is used on "I".
  Locale.setDefault(Locale.forLanguageTag("tr-TR"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void getValuesInTurkey() {
  Locale oldLocale = Locale.getDefault();
  Locale.setDefault(new Locale("tr", ""));
  try {
    Constants c = new Constants(A.class);
    Set<?> values = c.getValues("");
    assertEquals(7, values.size());
    assertTrue(values.contains(Integer.valueOf(0)));
    assertTrue(values.contains(Integer.valueOf(66)));
    assertTrue(values.contains(""));
    values = c.getValues("D");
    assertEquals(1, values.size());
    assertTrue(values.contains(Integer.valueOf(0)));
    values = c.getValues("prefix");
    assertEquals(2, values.size());
    assertTrue(values.contains(Integer.valueOf(1)));
    assertTrue(values.contains(Integer.valueOf(2)));
    values = c.getValuesForProperty("myProperty");
    assertEquals(2, values.size());
    assertTrue(values.contains(Integer.valueOf(1)));
    assertTrue(values.contains(Integer.valueOf(2)));
  }
  finally {
    Locale.setDefault(oldLocale);
  }
}

代码示例来源:origin: stackoverflow.com

public void onConfigurationChanged(Configuration newConfig)
{
  super.onConfigurationChanged(newConfig);
  // Set correct language (default or chosen)
  Locale locale = new Locale(getState()); 
  Locale.setDefault(locale);
  Configuration config = new Configuration(newConfig); // get Modifiable Config from actual config changed
  config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}

代码示例来源:origin: stackoverflow.com

Locale.setDefault(locale);
  getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
  locale = new Locale(lang);
  Locale.setDefault(locale);
  config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

代码示例来源:origin: org.apache.commons/commons-lang3

@BeforeClass
public static void classSetUp() {
  DEFAULT_LOCALE_BEFORE_TEST = Locale.getDefault();
  if (!DEFAULT_LOCALE_BEFORE_TEST.equals(Locale.CANADA)) {
    Locale.setDefault(Locale.CANADA);
  } else {
    // you seem to be from Canada...
    Locale.setDefault(Locale.CHINESE);
  }
  TEST_DEFAULT_LOCALE = Locale.getDefault();
  DEFAULT_TIMEZONE_BEFORE_TEST = TimeZone.getDefault();
  final TimeZone utc = FastTimeZone.getGmtTimeZone();
  if (!DEFAULT_TIMEZONE_BEFORE_TEST.equals(utc)) {
    TimeZone.setDefault(utc);
  } else {
    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
  }
  TEST_DEFAULT_TIMEZONE = TimeZone.getDefault();
}

代码示例来源:origin: spring-projects/spring-security

@Test
  public void resolvePermissionNonEnglishLocale() {
    Locale systemLocale = Locale.getDefault();
    Locale.setDefault(new Locale("tr"));

    AclService service = mock(AclService.class);
    AclPermissionEvaluator pe = new AclPermissionEvaluator(service);
    ObjectIdentity oid = mock(ObjectIdentity.class);
    ObjectIdentityRetrievalStrategy oidStrategy = mock(ObjectIdentityRetrievalStrategy.class);
    when(oidStrategy.getObjectIdentity(any(Object.class))).thenReturn(oid);
    pe.setObjectIdentityRetrievalStrategy(oidStrategy);
    pe.setSidRetrievalStrategy(mock(SidRetrievalStrategy.class));
    Acl acl = mock(Acl.class);

    when(service.readAclById(any(ObjectIdentity.class), anyList())).thenReturn(acl);
    when(acl.isGranted(anyList(), anyList(), eq(false))).thenReturn(true);

    assertThat(pe.hasPermission(mock(Authentication.class), new Object(), "write")).isTrue();

    Locale.setDefault(systemLocale);
  }
}

代码示例来源:origin: stackoverflow.com

Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);

代码示例来源:origin: stackoverflow.com

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)
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Before
public void setup() {
  results.clear();
  defaultLocale = Locale.getDefault(Locale.Category.FORMAT);
  Locale.setDefault(Locale.Category.FORMAT, java.util.Locale.US);
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testLocaleIndependence() throws Exception {
  final StringEncoder encoder = this.getStringEncoder();
  final String[] data = {"I", "i",};
  final Locale orig = Locale.getDefault();
  final Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()};
  try {
    for (final String element : data) {
      String ref = null;
      for (int j = 0; j < locales.length; j++) {
        Locale.setDefault(locales[j]);
        if (j <= 0) {
          ref = encoder.encode(element);
        } else {
          String cur = null;
          try {
            cur = encoder.encode(element);
          } catch (final Exception e) {
            Assert.fail(Locale.getDefault().toString() + ": " + e.getMessage());
          }
          Assert.assertEquals(Locale.getDefault().toString() + ": ", ref, cur);
        }
      }
    }
  } finally {
    Locale.setDefault(orig);
  }
}

相关文章