Skip to content

Instantly share code, notes, and snippets.

@st-kurilin
Last active December 24, 2015 17:09
Show Gist options
  • Save st-kurilin/6832917 to your computer and use it in GitHub Desktop.
Save st-kurilin/6832917 to your computer and use it in GitHub Desktop.
Java localization demo. Demonstrates: * localizing by putting values to files * Using more general description when specific is not available * Winning of more specific locale description * Getting resource bundles outside classpath * Changing resource bundles on runtime No third party dependencies, just core java. Written just for demo purpose…
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Java localization demo.
*/
public class Runner {
/**
* Run for demo.
*/
public static void main(String[] args) {
fillData(); //prepare files
demoRead(); //read with different locales
writeDemo(); //check changing property files
}
/**
* Fills files with some fake data.
*/
private static void fillData() {
// file values keys
fill("r", "default", "foo1", "foo3");
fill("r_en", "en", "foo2", "foo3");
fill("r_en_US", "en_US", "foo3", "foo4");
fill("r_en_US_ABCLTD", "en_US_ABCLTD", "foo3", "foo4");
fill("r_en__ABCLTD", "en__ABCLTD", "foo5");
}
/**
* Showing basics of reading values from bundles.
*/
private static void demoRead() {
//Using default: presented in r, r_en. Fr locale. r wins
print(loadBundle(new Locale("fr")).getString("foo1")); //=>default
//More specific wins: presented in r_en. en_US locale. r_en_US wins
print(loadBundle(new Locale("en", "US")).getString("foo2")); //=>en
//More specific wins(variant example): presented in r, r_en, en_US_ABCLTD. en_US_ABCLTD locale. r_en_US_ABCLTD wins
print(loadBundle(new Locale("en", "US", "ABCLTD")).getString("foo3")); //=>en_US_ABCLTD
//Using less specific: presented in r_en_US. en_US_ABCLTD locale. r_en wins
print(loadBundle(new Locale("en", "US", "ABCLTD")).getString("foo4")); //=>en_US
//Using variant without country: presented in r_en__ABCLTD. en__ABCLTD locale. r_en__ABCLTD wins
print(loadBundle(new Locale("en", "", "ABCLTD")).getString("foo5")); //=>en__ABCLTD
//Doesn't work. Could not use r_en__ABCLTD for en_US_ABCLTD locale
//print(loadBundle(new Locale("en", "US", "ABCLTD")).getString("foo5")); //=>en__ABCLTD
}
/**
* Showing changing properties during runtime.
*/
private static void writeDemo() {
fill("r", "default", "foo1", "foo3");
print(loadBundle(new Locale("fr")).getString("foo1")); //=>default
fill("r", "overriden", "foo1", "foo3");
print(loadBundle(new Locale("fr")).getString("foo1")); //=>overriden
}
/**
* Getting resource bundles from file system (outside jar/classpath).
*/
private static ResourceBundle loadBundle(Locale locale) {
try {
File file = new File(RESOURCES_LOCATION);
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
return ResourceBundle.getBundle(RESOURCE_NAME, locale, loader);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
/**
* Fills property file with name fileName with properties from keys with same value.
* (Very dummy impl)
*
* @param fileName property file
* @param value value for properties
* @param keys properties
*/
private static void fill(String fileName, String value, String... keys) {
try {
new File(RESOURCES_LOCATION).mkdirs();
final File file = new File(RESOURCES_LOCATION, fileName + ".properties");
file.delete();
file.createNewFile();
final PrintWriter writer = new PrintWriter(file);
for (String each : keys) {
writer.println(String.format("%s=%s", each, value));
}
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Just helper method.
*/
private static void print(String val) {
System.out.println(val);
}
/**
* Basic resource name.
*/
private static final String RESOURCE_NAME = "r";
/**
* Work directory. Should be cleaned after experimenting.
*/
private static final String RESOURCES_LOCATION = "D:\\localization";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment