eu.livotov.tpt.i18n
Class Dictionary

java.lang.Object
  extended by eu.livotov.tpt.i18n.Dictionary
All Implemented Interfaces:
java.io.Serializable

public class Dictionary
extends java.lang.Object
implements java.io.Serializable

A dictionary object, that holds translation data for all instances of a single application. In most cases this class shoud not be instantiated directly, use a TM front-end instead.

See Also:
Serialized Form

Field Summary
protected  java.util.HashMap<java.lang.String,java.util.Properties> countries
          Contains references to loaded properties files for the set of countries
private  java.lang.String defaultLanguage
          Default language.
protected  java.util.HashMap<java.lang.String,java.util.Properties> languages
          Contains references to loaded properties files for the set of languages
 
Constructor Summary
Dictionary()
           
 
Method Summary
 void clear()
           
protected  java.lang.String extractCountryCodeFromString(java.lang.String name)
           
protected  java.lang.String extractLanguageCodeFromString(java.lang.String name)
           
 java.lang.String get(com.vaadin.Application app, java.lang.String key)
          Provides a translation of the given key for the given application instance, using this instance language (locale).
 java.lang.String get(java.util.Locale locale, java.lang.String key)
          Provides a translation of the given key for the given locale
 java.lang.String get(java.lang.String key)
          Provides a translation of the given key for the current application instance, using this instance language (locale).
 java.lang.String get(java.lang.String lang, java.lang.String country, java.lang.String key)
          Provides a translation of the given key for the given language and country codes
 java.lang.String getDefaultLanguage()
          Provides current default language 2 letter code that is used by the dictionary in case key being queried is not found in the target language.
private  java.util.Properties getLanguageBundle(java.lang.String lang, java.lang.String country)
           
 void loadTranslationFilesFromThemeFolder(java.io.File themeFolder)
           
 void loadTranslationsFromLanguageDirectory(java.io.File folder)
           
 void loadWords(java.lang.String lang, java.lang.String country, java.io.File file, boolean overwrite)
          Loads data into dictionary from a reader object.
 void loadWords(java.lang.String lang, java.lang.String country, java.io.InputStream data)
          Loads data into dictionary from a property file.
 void loadWords(java.lang.String lang, java.lang.String country, java.io.Reader data)
          Loads data into dictionary from a reader object.
 void loadWords(java.lang.String lang, java.lang.String country, java.net.URL resource, boolean overwrite)
          Loads data into dictionary from a property file.
 void setDefaultLanguage(java.lang.String defaultLanguage)
          Sets the new current language 2 letter code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

languages

protected java.util.HashMap<java.lang.String,java.util.Properties> languages
Contains references to loaded properties files for the set of languages


countries

protected java.util.HashMap<java.lang.String,java.util.Properties> countries
Contains references to loaded properties files for the set of countries


defaultLanguage

private java.lang.String defaultLanguage
Default language. If translation key for the specified language is not found, a default language will be tried and only if it also fails, a key name is returned instead.

Constructor Detail

Dictionary

public Dictionary()
Method Detail

get

public java.lang.String get(java.lang.String key)
Provides a translation of the given key for the current application instance, using this instance language (locale). Note, that this method will only work if your application extends ToolkitPlusApplication. If your application is based in plain ITMill Toolkit Application, please use another version of this method witth the additional parameter.

Parameters:
key - key to get translated string for
Returns:
translated string or the key name itself, if no translated string found

get

public java.lang.String get(com.vaadin.Application app,
                            java.lang.String key)
Provides a translation of the given key for the given application instance, using this instance language (locale).

Parameters:
app - application instance. It is used to extact locale to be used in translation
key - key to get translated string for
Returns:
translated string or the key name itself, if no translated string found

get

public java.lang.String get(java.util.Locale locale,
                            java.lang.String key)
Provides a translation of the given key for the given locale

Parameters:
locale - locale
key - key to get translated string for
Returns:
translated string or the key name itself, if no translated string found

get

public java.lang.String get(java.lang.String lang,
                            java.lang.String country,
                            java.lang.String key)
Provides a translation of the given key for the given language and country codes

Parameters:
lang - language 2 letter code. Cannot be nullor empty
country - country 2 letter code. Can be null or empty, in this case, generic translation for the country is used.
key - key to get translated string for
Returns:
translated string or the key name itself, if no translated string found

loadWords

public void loadWords(java.lang.String lang,
                      java.lang.String country,
                      java.net.URL resource,
                      boolean overwrite)
               throws java.io.IOException
Loads data into dictionary from a property file.

Parameters:
lang - 2 letter language code, the file being loaded refers to. Cannot be null or empty.
country - 2 letter country code, the file being loaded rerefs to. Can be null or empty.
resource - URL to a property file from classpath resource. Note, that resource contents must be in UTF-8 encoding
overwrite - if set to false, dictionary will allow any unique url to be loaded only once. This is useful ehrn you load translations in the application init or constructor method, which is called for all sessions. Having overwrite parameter set to false, you prevents spending system resources for unneeded loads of the same data.
Throws:
java.io.IOException - if any IO error occurs

loadWords

public void loadWords(java.lang.String lang,
                      java.lang.String country,
                      java.io.InputStream data)
               throws java.io.IOException
Loads data into dictionary from a property file.

Parameters:
lang - 2 letter language code, the file being loaded refers to. Cannot be null or empty.
country - 2 letter country code, the file being loaded rerefs to. Can be null or empty.
data - input stream for the translation property file contents to be loaded.
Throws:
java.io.IOException - if any IO error occurs

loadWords

public void loadWords(java.lang.String lang,
                      java.lang.String country,
                      java.io.Reader data)
               throws java.io.IOException
Loads data into dictionary from a reader object. This allow to eliminate using native2ascii utility for non-english contents of i18n files

Parameters:
lang - 2 letter language code, the file being loaded refers to. Cannot be null or empty.
country - 2 letter country code, the file being loaded rerefs to. Can be null or empty.
data - reader instance for the translation property file contents to be loaded.
Throws:
java.io.IOException - if any IO error occurs

loadWords

public void loadWords(java.lang.String lang,
                      java.lang.String country,
                      java.io.File file,
                      boolean overwrite)
               throws java.io.IOException
Loads data into dictionary from a reader object. This allow to eliminate using native2ascii utility for non-english contents of i18n files

Parameters:
lang - 2 letter language code, the file being loaded refers to. Cannot be null or empty.
country - 2 letter country code, the file being loaded rerefs to. Can be null or empty.
file - i18n file. Note, the contents must be in UTF-8 encoding.
Throws:
java.io.IOException - if any IO error occurs

getLanguageBundle

private java.util.Properties getLanguageBundle(java.lang.String lang,
                                               java.lang.String country)

getDefaultLanguage

public java.lang.String getDefaultLanguage()
Provides current default language 2 letter code that is used by the dictionary in case key being queried is not found in the target language.

Returns:
current language 2 letter code

setDefaultLanguage

public void setDefaultLanguage(java.lang.String defaultLanguage)
Sets the new current language 2 letter code.

Parameters:
defaultLanguage - new 2 letter code to be used as a current language.

loadTranslationFilesFromThemeFolder

public void loadTranslationFilesFromThemeFolder(java.io.File themeFolder)
                                         throws java.io.IOException
Throws:
java.io.IOException

loadTranslationsFromLanguageDirectory

public void loadTranslationsFromLanguageDirectory(java.io.File folder)
                                           throws java.io.IOException
Throws:
java.io.IOException

extractCountryCodeFromString

protected java.lang.String extractCountryCodeFromString(java.lang.String name)

extractLanguageCodeFromString

protected java.lang.String extractLanguageCodeFromString(java.lang.String name)

clear

public void clear()