Skip to main content

i18n getting all translated languages

1 reply [Last post]
Jens Hohl
Offline
Joined: 2006-02-17

Hi,

i've tested a bit to make a Swing App International,
all is fine so far.I loaded all language specific files
over PropertyResourceBundle. How is the easiest way to get all languages that were translated to let the user
choose it at runtime ?

PropertyResourceBundle bundle = PropertyResourceBundle(inpuStream);

loads a bundle, but a call to bundle.getLoacle();
returns null, i first have to know the languages to get a valid value there, what is the proper way to get all Languages translated for a Application ? The underlying mechanism to locate all translation is build in the ResourceBundle class i know, how to interate over all the languages ?

Thx,
Jens

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
eduardj
Offline
Joined: 2004-06-21

I had the same problem as you, and couldn't solve it using standard Java API calls. If you find the solution, please post it or tell me directly.

Meanwhile, I worked out some code to find out the languages the hard way: exploring the classpath directly. It works no matter whether the application is packaged in a jar file or not. The main method is called computeLocales(String), and the parameter is the base name of the resource files, without ".properties". It has a couple of limitations: 1- the resource files must be all in the same folder/package; 2- there must be a default resource file, being this default file ".properties" without locale id's. I hope it'll work for you. If you use it or improve it, tell me!

public static Locale[] computeLocales(final String ref)
{
ArrayList toReturn = new ArrayList();
URL res = ResourceData.class.getClassLoader().getResource(ref+".properties");
if ("jar".equals(res.getProtocol()))
{
String file = res.getFile();
try
{
JarFile jarFile = new JarFile(file.substring("file:".length(), file.indexOf('!')));
Enumeration enum = jarFile.entries();
while (enum.hasMoreElements())
{
ZipEntry entry = (ZipEntry)enum.nextElement();
String name = entry.getName();
if (name.startsWith(ref+"_") && name.endsWith(".properties"))
{
String localeName = name.substring((ref+"_").length(), name.indexOf(".properties"));
toReturn.add(parseLocale(localeName));
}
}
jarFile.close();
}
catch(IOException _ioe)
{
_ioe.printStackTrace();
}
}
else
{
String filename = res.getFile();
try{filename = URLDecoder.decode(filename, "UTF8");}catch(UnsupportedEncodingException _uee){_uee.printStackTrace();}
File file = new File(filename).getParentFile();
final String refName = new File(ref).getName();
File[] files = file.listFiles(new FilenameFilter()
{
public boolean accept(File arg0, String name)
{
//System.out.println(name);
return name.endsWith(".properties") && name.startsWith(refName+"_");
}
});
for (int iCnt=0; iCnt {
String name = files[iCnt].getName();
toReturn.add(parseLocale(name.substring(refName.length()+1, name.lastIndexOf(".properties"))));
}
}
return (Locale[])toReturn.toArray(new Locale[toReturn.size()]);
}

private static Locale parseLocale(String localeName)
{
StringTokenizer parser = new StringTokenizer(localeName, "_");
Locale locale;
String language = parser.nextToken();
if (parser.hasMoreTokens())
{
String country = parser.nextToken();
if (parser.hasMoreTokens())
locale = new Locale(language, country, parser.nextToken());
else
locale = new Locale(language, country);
}
else
locale = new Locale(language);
return locale;
}