-
-
Save ffbit/4370531 to your computer and use it in GitHub Desktop.
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 org.test; | |
| import java.io.File; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.net.URLClassLoader; | |
| import java.util.Locale; | |
| import java.util.MissingResourceException; | |
| import java.util.ResourceBundle; | |
| /** | |
| * Load localized messages from external folder. | |
| */ | |
| public class MessageLoader { | |
| private static final String RESOURCE_DIR = "/opt/messages"; | |
| private ResourceBundle bundle = null; | |
| public MessageLoader(String baseName, String lang) { | |
| String language = lang; | |
| if (language == null) { | |
| language = ""; | |
| } | |
| try { | |
| loadBundle(baseName, language); | |
| } catch (Exception e) { | |
| System.out.println("can't load resource for baseName = " + baseName + " language = " + language); | |
| } | |
| } | |
| /** | |
| * Returns message specified by key from current resource file. | |
| * | |
| * @param key | |
| * key to specify a message | |
| * @return localized message | |
| */ | |
| public String getString(String key) { | |
| if (bundle == null) { | |
| return ""; | |
| } | |
| String value = ""; | |
| try { | |
| value = bundle.getString(key); | |
| } catch (MissingResourceException e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| return value; | |
| } | |
| /** | |
| * Load resource bundle for specified base name and language. <br /> | |
| * Find resource files in external folder specified by RESOURCE_DIR field. <br /> | |
| * Find resource file in the following sequence: <br /> | |
| * - <i>baseName + lang</i><br /> | |
| * - <i>baseName</i><br /> | |
| * Using ResourceBundle.Control allows to exclude | |
| * <i>baseName + defaultLocale</i> from the candidate bundle names. | |
| * | |
| * @param baseName | |
| * base name for resource file | |
| * @param lang | |
| * language | |
| * @throws MalformedURLException | |
| * incorrect resource directory name | |
| */ | |
| private void loadBundle(String baseName, String lang) throws MalformedURLException { | |
| File resourceDir = new File(RESOURCE_DIR); | |
| URL[] urls = { resourceDir.toURI().toURL() }; | |
| ClassLoader loader = new URLClassLoader(urls); | |
| Locale locale = new Locale(lang); | |
| ResourceBundle.Control control = ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES); | |
| bundle = ResourceBundle.getBundle(baseName, locale, loader, control); | |
| } | |
| /** | |
| * Prints 'Hello Welt!' from messages_de.properties. <br /> | |
| * Prints 'Hello world! (default)' from default messages.properties | |
| * because messages_ru.properties was not found. | |
| */ | |
| public static void main(String[] args) { | |
| MessageLoader messageLoader = new MessageLoader("messages", "de"); | |
| System.out.println(messageLoader.getString("hello")); // Hallo Welt! | |
| messageLoader = new MessageLoader("messages", "ru"); | |
| System.out.println(messageLoader.getString("hello")); // Hello world! (default) | |
| } | |
| } |
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
| hello=Hello world! (default) |
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
| hello=Hallo Welt! |
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
| hello=Hello world! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment