本文整理汇总了Java中org.springframework.context.i18n.LocaleContextHolder.setLocale方法的典型用法代码示例。如果您正苦于以下问题:Java LocaleContextHolder.setLocale方法的具体用法?Java LocaleContextHolder.setLocale怎么用?Java LocaleContextHolder.setLocale使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.context.i18n.LocaleContextHolder
的用法示例。
在下文中一共展示了LocaleContextHolder.setLocale方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: service
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
LocaleContextHolder.setLocale(request.getLocale());
try {
this.target.handleRequest(request, response);
}
catch (HttpRequestMethodNotSupportedException ex) {
String[] supportedMethods = ex.getSupportedMethods();
if (supportedMethods != null) {
response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
}
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
示例2: testBindingErrorWithFormatter
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testBindingErrorWithFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertEquals(new Float(0.0), tb.getMyFloat());
assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
示例3: testBindingErrorWithFormatterAgainstFields
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testBindingErrorWithFormatterAgainstFields() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.initDirectFieldAccess();
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertEquals(new Float(0.0), tb.getMyFloat());
assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
示例4: testBindingErrorWithCustomFormatter
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testBindingErrorWithCustomFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.addCustomFormatter(new NumberStyleFormatter());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1x2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertEquals(new Float(0.0), tb.getMyFloat());
assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
示例5: testAmountAndUnit
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testAmountAndUnit() {
MoneyHolder bean = new MoneyHolder();
DataBinder binder = new DataBinder(bean);
binder.setConversionService(conversionService);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("amount", "USD 10.50");
propertyValues.add("unit", "USD");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
示例6: testAmountWithNumberFormat5
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testAmountWithNumberFormat5() {
FormattedMoneyHolder5 bean = new FormattedMoneyHolder5();
DataBinder binder = new DataBinder(bean);
binder.setConversionService(conversionService);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("amount", "USD 10.50");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
LocaleContextHolder.setLocale(Locale.CANADA);
binder.bind(propertyValues);
LocaleContextHolder.setLocale(Locale.US);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
示例7: testDefaultFormattersOn
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testDefaultFormattersOn() throws Exception {
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
factory.afterPropertiesSet();
FormattingConversionService fcs = factory.getObject();
TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
Object value = fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
assertEquals(15.0, value);
value = fcs.convert(15.0, descriptor, TypeDescriptor.valueOf(String.class));
assertEquals("15", value);
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:FormattingConversionServiceFactoryBeanTests.java
示例8: requestInitialized
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
throw new IllegalArgumentException(
"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
}
HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
LocaleContextHolder.setLocale(request.getLocale());
RequestContextHolder.setRequestAttributes(attributes);
}
示例9: initContextHolders
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
private void initContextHolders(HttpServletRequest request, ServletRequestAttributes requestAttributes) {
LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
if (logger.isDebugEnabled()) {
logger.debug("Bound request context to thread: " + request);
}
}
示例10: setUserTenantContext
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
private String setUserTenantContext(HttpServletRequest request) {
String tenant;
final OAuth2Authentication auth = getAuthentication();
if (auth == null) {
tenant = request.getHeader(Constants.HEADER_TENANT);
TenantContext.setCurrent(tenant);
} else {
Map<String, String> details = null;
if (auth.getDetails() != null) {
details = Map.class.cast(OAuth2AuthenticationDetails.class.cast(auth.getDetails())
.getDecodedDetails());
}
details = firstNonNull(details, new HashMap<>());
tenant = details.getOrDefault(AUTH_TENANT_KEY, "");
String xmToken = details.getOrDefault(AUTH_XM_TOKEN_KEY, "");
String xmCookie = details.getOrDefault(AUTH_XM_COOKIE_KEY, "");
String xmUserId = details.getOrDefault(AUTH_XM_USERID_KEY, "");
String xmLocale = details.getOrDefault(AUTH_XM_LOCALE_KEY, "");
String xmUserLogin = (String) auth.getPrincipal();
TenantContext.setCurrent(new TenantInfo(tenant, xmToken, xmCookie, xmUserId, xmLocale, xmUserLogin));
Locale locale = LocaleUtils.getLocaleFromString(xmLocale);
if (locale != null) {
LocaleContextHolder.setLocale(locale);
}
}
return tenant;
}
示例11: setUp
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
private void setUp(DateFormatterRegistrar registrar) {
DefaultConversionService.addDefaultConverters(conversionService);
registrar.registerFormatters(conversionService);
SimpleDateBean bean = new SimpleDateBean();
bean.getChildren().add(new SimpleDateBean());
binder = new DataBinder(bean);
binder.setConversionService(conversionService);
LocaleContextHolder.setLocale(Locale.US);
}
示例12: testBindingWithCustomFormatter
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void testBindingWithCustomFormatter() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
binder.addCustomFormatter(new NumberStyleFormatter(), Float.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertEquals(new Float(1.2), tb.getMyFloat());
assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));
PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
assertNotNull(editor);
editor.setValue(new Float(1.4));
assertEquals("1,4", editor.getAsText());
editor = binder.getBindingResult().findEditor("myFloat", null);
assertNotNull(editor);
editor.setAsText("1,6");
assertTrue(((Number) editor.getValue()).floatValue() == 1.6f);
}
finally {
LocaleContextHolder.resetLocaleContext();
}
}
示例13: createValidator
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
public static Validator createValidator() {
LocaleContextHolder.setLocale(Locale.ENGLISH);
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setFallbackToSystemLocale(false);
messageSource.setBasenames("validation");
messageSource.setDefaultEncoding("utf8");
LocalValidatorFactoryBean localValidator = new LocalValidatorFactoryBean();
localValidator.setValidationMessageSource(messageSource);
localValidator.afterPropertiesSet();
return localValidator;
}
示例14: getAllEnglish
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void getAllEnglish() {
LocaleContextHolder.setLocale(Locale.ENGLISH);
final Map<String, List<NamedDto<String>>> all = clientEnumController.getAll();
Assert.assertEquals(1, all.size());
final List<NamedDto<String>> gender = all.get("Gender");
Assert.assertEquals(2, gender.size());
Assert.assertEquals("female", gender.get(1).getName());
Assert.assertEquals("F", gender.get(1).getId());
}
示例15: resolveI18nPartialLocale
import org.springframework.context.i18n.LocaleContextHolder; //导入方法依赖的package包/类
@Test
public void resolveI18nPartialLocale() throws Exception {
LocaleContextHolder.setLocale(Locale.FRANCE);
URL url = this.configurer.resolveTemplate(getClass().getClassLoader(), TEMPLATE_PREFIX + "i18n.tpl");
Assert.assertNotNull(url);
Assert.assertThat(url.getPath(), Matchers.containsString("i18n_fr.tpl"));
}