public class I18n
extends java.lang.Object
This class can be used to localize Strings in the application. Localized Strings are stored inside .properties files (sets of KEY=Localized phrase) inside a localization folder. Localization folders have several .properties files with filenames corresponding to classes that have localization needs. The file 'global.properties' is a special properties file that will always be looked at before going into the class-specific properties file.
To start using localization, you simply need to start calling the static methods #I18n.text(String, String)
and
#I18n.textf(String, String, String...)
. The corresponding .properties files together with english-default phrases
will automatically be created.
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
appendedText |
protected java.lang.String |
defaultLanguage |
static java.lang.String |
I18N_BASE_LOCALIZATION |
protected static I18n |
instance |
protected java.lang.String |
language |
static java.util.regex.Pattern |
localeStringPattern |
protected PoFile |
po |
protected static java.util.regex.Pattern |
substitutionStringPattern |
Modifier and Type | Method and Description |
---|---|
protected static I18n |
getInstance() |
protected void |
init() |
protected java.lang.String |
localize(java.lang.String key,
java.lang.String englishDefault) |
protected java.lang.String |
localizeAdvanced(java.lang.String key,
java.lang.String englishDefault,
java.lang.Class<?> requester) |
static void |
main(java.lang.String[] args) |
static java.lang.String |
normalize(java.lang.String text) |
static void |
setDebugPrefix(java.lang.String prefix)
Change the text that gets appended to all localized Strings (used only in debug)
|
static java.lang.String |
text(java.lang.String englishDefault)
This method allows localization of a (simple) phrase whose key can be automatically generated.
|
static java.lang.String |
textAdvanced(java.lang.String key,
java.lang.String englishDefault,
java.lang.Class<?> requester)
Localize the String by strictly setting the name of the class that is requesting the localization.
|
static java.lang.String |
textc(java.lang.String englishDefault,
java.lang.String context)
Same as
text(String) but with an extra id containing a context for the translation. |
static java.lang.String |
textf(java.lang.String englishDefault)
Deprecated.
|
static java.lang.String |
textf(java.lang.String englishDefault,
java.lang.Object... parameters)
This method provides parametrized localization.
|
static java.lang.String |
textfc(java.lang.String englishDefault,
java.lang.String context)
Deprecated.
|
static java.lang.String |
textfc(java.lang.String englishDefault,
java.lang.String context,
java.lang.Object... parameters)
Same as
textf(String) but with an extra id containing a context. |
static java.lang.String |
textmark(java.lang.String englishDefault)
This method marks a string to be translated
|
static java.lang.String |
textmarkc(java.lang.String englishDefault,
java.lang.String context)
This method marks a string to be translated with context
|
public static final java.lang.String I18N_BASE_LOCALIZATION
protected java.lang.String defaultLanguage
protected java.lang.String language
protected static java.util.regex.Pattern substitutionStringPattern
protected static I18n instance
protected static java.lang.String appendedText
public static java.util.regex.Pattern localeStringPattern
protected PoFile po
protected void init()
protected static I18n getInstance()
protected java.lang.String localize(java.lang.String key, java.lang.String englishDefault)
protected java.lang.String localizeAdvanced(java.lang.String key, java.lang.String englishDefault, java.lang.Class<?> requester)
public static java.lang.String normalize(java.lang.String text)
public static java.lang.String text(java.lang.String englishDefault)
"This is a just a (very) simple test!" -> "THIS_IS_JUST_A__VERY__SIMPLE_TEST_"
englishDefault
- The (simple) text to be localized.public static java.lang.String textc(java.lang.String englishDefault, java.lang.String context)
text(String)
but with an extra id containing a context for the translation. E.g. "verb", "referring to a system"englishDefault
- The (simple) text to be localized.context
- The context for the translation@Deprecated public static java.lang.String textf(java.lang.String englishDefault)
textf(String, Object...)
@Deprecated public static java.lang.String textfc(java.lang.String englishDefault, java.lang.String context)
#textfc(String, Object...)
public static java.lang.String textf(java.lang.String englishDefault, java.lang.Object... parameters)
String text1 = I18n.textf("FILE_NOT_READABLE", "The file '%filename' is not readable", "/home/zp/file.txt"); //results in "The file '/home/zp/file.txt' is not readable" for english default String text2 = I18n.textf("Loading is %percent% done", 10.4); //results in "Loading is 10.4% done" for english default String text3 = I18n.textf("%s1 plus %s2 equals %s3", s1, s2, s1+s2);
englishDefault
- The default text in american englishparameters
- A list of Objects that will be replaced in the localized text (method to toString() is called on each one)public static java.lang.String textfc(java.lang.String englishDefault, java.lang.String context, java.lang.Object... parameters)
textf(String)
but with an extra id containing a context. E.g. "verb", "referring to a system"englishDefault
- The default text in american englishcontext
- The context for the translationparameters
- A list of Objects that will be replaced in the localized text (method to toString() is called on each one)public static java.lang.String textmark(java.lang.String englishDefault)
englishDefault
- public static java.lang.String textmarkc(java.lang.String englishDefault, java.lang.String context)
englishDefault
- context
- public static java.lang.String textAdvanced(java.lang.String key, java.lang.String englishDefault, java.lang.Class<?> requester)
key
- The key that will be common to all languages. Usage of non-ascii characters is disencouragedenglishDefault
- Default english textrequester
- The class that will be used for looking up the textpublic static void setDebugPrefix(java.lang.String prefix)
prefix
- The prefix String to be appended to all Stringspublic static void main(java.lang.String[] args)
Copyright © 2004-2020 FEUP-LSTS and Neptus developers. All Rights Reserved.