
BBTranslationBundle
Description
In BBj 9.0 and higher, the BBTranslationBundle utility is a BBj CustomObject used to get and add translations with backend Java properties files for localization support.
Creation
The BBTranslationBundle class can be found in <bbj install>/utils/translations/bbtranslator.bbj.
BBTranslationBundle.getBundle(string name) |
BBTranslationBundle.getBundle(string name, string directory) |
BBTranslationBundle.getBundle(string name, string directory, locale localeOfOrigin) |
Parameters
Parameter |
Description |
name |
Specifies the name of the translation bundle. |
translationBundle |
Specifies the translation bundle the translator will use. |
localeOfOrigin |
Specifies the Java locale for the default translations. |
Methods of BBTranslationBundle
Return Value |
Method |
void |
addLocale(Locale locale) |
void |
addTranslation(string key, string value) |
void |
addTranslation(string key, HashMap values) |
void |
addTranslation(Locale locale, string key, string value) |
void |
|
static void |
compareBundles(string bundleName, string bundleDir1, string bundleDir2, boolean displayToConsole) |
string |
|
Locale |
|
string |
getName() |
string |
getTranslation(string key) |
string |
getTranslation(string key, string defaultValue) |
string |
getTranslation(string key, string defaultValue, boolean addIfNotFound) |
string |
getTranslation(Locale locale, string key) |
string |
getTranslation(Locale locale, string key, string defaultValue) |
string |
getTranslation(Locale locale, string key, string defaultValue, boolean addIfNotFound) |
getTranslations(Locale locale) |
|
void |
removeLocale(Locale locale) |
void |
save() |
void |
save(string directoryName) |
static void |
writePropertiesFile(string propertiesFileName, Properties properties) |
static void |
validateBundle(string bundleName, string bundleDirName) |
static void |
validateBundle(string bundleName, string bundleDirName, string reportName) |
static void |
validateBundle(string bundleName, string bundleDirName, string reportName, boolean removeInvalid) |
Remarks
None.
Constants
Name |
Value |
NAME_ERROR |
256 |
REMOVE_LOCALE_ERROR |
257 |
DIR_ERROR |
258 |
SAVE_ERROR |
259 |
Example
rem Demo on how to use BBTranslator, BBTranslationBundle, and BBTranslations
rem Use statements
use ::bbtranslator.bbj::BBTranslator
use ::bbtranslator.bbj::BBTranslationBundle
use ::bbtranslator.bbj::BBTranslations
use java.lang.System
use java.util.Locale
use java.util.HashMap
use java.util.Collection
use java.util.Iterator
rem Declares
declare BBjAPI api!
declare BBjString bundleDir$
declare BBjString bundleName$
declare BBTranslationBundle bundle!
declare BBTranslator germanTranslator!
declare Collection translationsKeys!
declare Iterator translationsIter!
declare BBjString translationsKey$
declare BBjString translation$
declare BBTranslations enTranslations!
declare HashMap newTranslations!
rem Get a reference to the BBjAPI
api! = BBjAPI()
rem Set the bundle name
bundleName$ = "sample"
rem Use the translations directory for the bundle directory
bundleDir$ = System.getProperty("basis.BBjHome") + "/utils/translations/"
rem Create the bundle
bundle! = BBTranslationBundle.getBundle(bundleName$,bundleDir$)
rem There are mutliple ways to add a translation
rem Add a translation one at a time to a particlular locale in the translation bundle.
enTranslations! = bundle!.getTranslations(new Locale("en"))
print "Adding Key1/Value1 individually..."
enTranslations!.addTranslation("Key1","Value1")
print "Adding Key2/Value2 individually..."
enTranslations!.addTranslation("Key2","Value2")
rem Add new translations via a hashmap to a particlular locale in the translation bundle
print "Adding Key3/Value3 and Key4/Key5 at once per HashMap..."
newTranslations! = new HashMap()
newTranslations!.put("Key3","Value3")
newTranslations!.put("Key4","Value4")
enTranslations!.addTranslations(newTranslations!)
rem Add a translation to multiple locales in the translation bundle
print "Adding the key Hello with values for multiple locales..."
declare HashMap newTranslation!
newTranslation! = new HashMap()
newTranslation!.put(new Locale("en"),"hello")
newTranslation!.put(new Locale("es"),"hola")
newTranslation!.put(new Locale("it"),"cioa")
newTranslation!.put(new Locale("fr"),"bonjour")
newTranslation!.put(new Locale("nl"),"hallo")
newTranslation!.put(new Locale("de"),"guten tag")
newTranslation!.put(new Locale("sv"),"god dag")
bundle!.addTranslation("hello",newTranslation!)
rem Save the bundle
bundle!.save()
print "Translation bundle saved to: ",bundle!.getDirectory()
rem Get a translation from the bundle
rem A translator can be used for convience to get translations or a single translation from
rem a translation bundle on a per locale basis
germanTranslator! = BBTranslator.getInstance(bundleName$,"de",null(),bundleDir$)
rem To get a single translation
translationKey$ = "hello"
translation$ = germanTranslator!.getTranslation(translationKey$)
print "The German translation for key [",translationKey$,"] is [",translation$,"]"
rem To get all translation for a locale
translationsKeys! = enTranslations!.getKeys()
translationsIter! = translationsKeys!.iterator()
while translationsIter!.hasNext()
translationKey$ = str(translationsIter!.next())
translation$ = entranslations!.getTranslation(translationKey$)
print "The English translation for key [",translationKey$,"] is [",translation$,"]"
wend