tig
Class TigBundle
- java.lang.Object
-
- tig.TigBundle
-
- All Implemented Interfaces:
- GeneralConstants
public class TigBundle extends java.lang.Object implements GeneralConstants
This class can be used instead ofResourceBundle. It has been written for these reasons :- Permit to choose the place where the file containing the bundle (in general a '.properties') is stored ;
- Put the Strings of different languages in a same file.
- Avoid having Exceptions thrown when a key can't be found (the
getString(String)method returns a empty string if a key can't be found in the bundle).Usage
- As with the standard API, you put your internationalized strings in bundle files (which don't have to be suffixed by '.properties'). The difference is that all the strings related to the same display are located in the same file.
- You fill the bundle files with the standard syntax ("key = value")
- Like in the standard mechanism, languages, countries and variants are handled.
- The language and the locale are identified by the suffixes of the key ; the syntax is :
myKey_country_locale_variant(ex :myKey_en_US).
The constructors ofTigBundleonly store the necessary strings (corresponding to theLocalespecified in the constructor).- WARNING : this implementation does NOT permit to have underscores ( _ )in the names of the keys.
Example : if you internationalize with French and English, you can have in your file :myKey_en = myEnglishValue myKey_fr = myFrenchValue
But the following example is not valid, because of underscores :my_Key_en = myEnglishValue my_Key_fr = myFrenchValue
- Typical usage :
TigBundle myBundle = new TigBundle("C:\\data\\MyBundle.properties", new Locale("en", "US")); String str = myBundle.getString("myKey");- Alternative usage :
In this alternative usage
In the main constructor (TigBundle(String, Locale)), ajava.io.IOExceptionis thrown if the resource file can't be loaded. An other constructor is provided to be able to load "manually" the bundle : (TigBundle(Properties, Locale)).Properties p = new Properties(); p.setProperty("myKey_fr", "myFrenchValue"); p.setProperty("myKey", "myEnglishValue"); TigBundle myBundle = new TigBundle(p, new Locale("en", "US")); String str = myBundle.getString("myKey");Retrieval mechanism
WhengetString("myKey")is called, it tries to find, in this order :"myKey_country_locale_variant""myKey_country_locale""myKey_country""myKey"
If nothing is found, it returns a empty string.
This permits to avoid repeating strings shared by several language, and have default values :
Imagine you handle Spanish, French and English, and that the word "OK" should be expressed by "OK" in French and English, and "Oke" in spanish. You would put in the bundle :OK_sp = Oke OK = OK
-
-
Constructor Summary
Constructors Constructor and Description TigBundle(java.util.Properties p, java.util.Locale locale)Creates an bundle from a Propetries.TigBundle(java.lang.String pathName, java.util.Locale locale)Constructor to use in general.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.util.PropertiesgetData()Returns thePropertiesused to internally store the data.java.lang.StringgetString(java.lang.String key)Returns the value corresponding to parameter 'key' (see details of method).static voidmain(java.lang.String[] args)
-
-
-
Constructor Detail
-
TigBundle
public TigBundle(java.util.Properties p, java.util.Locale locale)Creates an bundle from a Propetries.
-
TigBundle
public TigBundle(java.lang.String pathName, java.util.Locale locale) throws java.io.IOExceptionConstructor to use in general.
When called, the data are loaded from the specified file.- Parameters:
pathName- Absolute path of the file containing the resources.locale- Locale of this bundle.- Throws:
java.lang.RuntimeException- If parameter 'locale' is null or if its language is not set (null or empty string).java.io.IOException
-
-
Method Detail
-
getString
public java.lang.String getString(java.lang.String key)
Returns the value corresponding to parameter 'key' (see details of method).
If the corresponding key does not exist, returns an empty String.
The search order for a corresponding key is similar as what is done in the standard API :
If the value of parameter 'key' is 'theKey' and theLocaleused to build this bundle contains a language 'la', a country 'co' and a variant 'va', will try to find a key, the following keys will be searched in the file :theKey_la_co_vatheKey_la_cotheKey_latheKey
If none of these keys are found, returns an empty String (doesn't throw an Exception as in Sun's API).- Parameters:
key- The key corresponding to the String to retrieve.
-
getData
public java.util.Properties getData()
Returns thePropertiesused to internally store the data.
-
main
public static void main(java.lang.String[] args)
-
-
DMelt 3.0 © DataMelt by jWork.ORG