Created
March 30, 2012 17:53
-
-
Save ussy/2253362 to your computer and use it in GitHub Desktop.
Localizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>example</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<packaging>jar</packaging> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.3.2</version> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
<encoding>UTF-8</encoding> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-eclipse-plugin</artifactId> | |
<version>2.8</version> | |
<configuration> | |
<downloadSources>true</downloadSources> | |
<downloadJavadocs>true</downloadJavadocs> | |
<additionalConfig> | |
<file> | |
<name>.settings/org.eclipse.core.resources.prefs</name> | |
<content> | |
<![CDATA[ | |
eclipse.preferences.version=1 | |
encoding/<project>=UTF-8 | |
]]> | |
</content> | |
</file> | |
<file> | |
<name>.settings/org.eclipse.core.runtime.prefs</name> | |
<content> | |
<![CDATA[ | |
eclipse.preferences.version=1 | |
line.separator=\n | |
]]> | |
</content> | |
</file> | |
</additionalConfig> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.jvnet.localizer</groupId> | |
<artifactId>maven-localizer-plugin</artifactId> | |
<version>1.12</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>generate</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>org.jvnet.localizer</groupId> | |
<artifactId>localizer</artifactId> | |
<version>1.12</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.util.Locale; | |
import org.jvnet.localizer.LocaleProvider; | |
public class Resources { | |
public static void main(String[] args) throws Exception { | |
final WebRequestLocaleProvider localeProvider = new WebRequestLocaleProvider(); | |
LocaleProvider.setProvider(localeProvider); | |
for (int i = 0; i < 100; i++) { | |
final int num = i; | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
Locale locale = num % 2 == 0 ? Locale.JAPAN : Locale.US; | |
localeProvider.alloc(locale); | |
System.out.println(locale + ":" + Messages.hello("world")); | |
localeProvider.release(); | |
} | |
}).start(); | |
} | |
Thread.sleep(1000); | |
} | |
static class WebRequestLocaleProvider extends LocaleProvider { | |
private ThreadLocal<Locale> locales = new ThreadLocal<Locale>(); | |
public void alloc(Locale locale) { | |
locales.set(locale); | |
} | |
public void release() { | |
locales.remove(); | |
} | |
@Override | |
public Locale get() { | |
return locales.get(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment