public class NLS extends Object
This class provides a translation engine (get(String)
, safeGet(String, String, String)
,
fmtr(String)
). It also provides access to the current language via getCurrentLang()
and to the
default language (getDefaultLanguage()
. Most of the methods come in two versions, one which accepts a
lang parameter and another which uses the currently active language.
Additionally this class provides conversion methods to and from String. The most prominent ones are
toUserString(Object)
and toMachineString(Object)
along with their equivalent parse methods.
Although some conversions, especially toMachineString or formatSize are not language dependent,
those are kept in this class, to keep all conversion methods together.
Babelfish is used as translation engine and responsible for loading all provided .properties files.
Configuration
Babelfish
Modifier and Type | Class and Description |
---|---|
static class |
NLS.CommonKeys
Provides access to commonly used keys.
|
Modifier and Type | Method and Description |
---|---|
static String |
convertDuration(long duration)
Converts the given duration in milliseconds including seconds and milliseconds
This is a boilerplate method for
convertDuration(long, boolean, boolean) with
includeSeconds and includeMillis set to true. |
static String |
convertDuration(long duration,
boolean includeSeconds,
boolean includeMillis)
Converts a given time range in milliseconds to a human readable format using the current language
|
static Formatter |
fmtr(String property)
Creates a formatted using the pattern supplied by the translation value for the given property.
|
static Formatter |
fmtr(String property,
String lang)
Creates a formatted using the pattern supplied by the translation value for the given property.
|
static String |
formatSize(long size)
Converts a file or byte size.
|
static String |
get(String property)
Returns a translated text for the given property and the currently active language.
|
static String |
get(String property,
int numeric)
Returns one of two or three versions of a translation based on the given numeric for the current language.
|
static String |
get(String property,
int numeric,
String lang)
Returns one of two or three versions of a translation based on the given numeric.
|
static String |
get(String property,
String lang)
Returns a translated text for the given property in the given language.
|
static String |
getCurrentLang()
Returns the currently active language as two-letter code.
|
static DateTimeFormatter |
getDateFormat(String lang)
Returns the date format for the given language.
|
static DateTimeFormatter |
getDateTimeFormat(String lang)
Returns the date and time format (without seconds) for the given language.
|
static String |
getDayOfWeek(int day)
Converts a given integer (
Calendar.Monday...Calendar.Sunday ) into textual their representation. |
static String |
getDayOfWeekShort(int day)
Returns a two letter abbreviation of the name of the given day, like
"Mo" . |
static NumberFormat |
getDecimalFormat()
Returns the format for the current language to format decimal numbers
|
static NumberFormat |
getDecimalFormat(String lang)
Returns the format for the given language to format decimal numbers
|
static DecimalFormatSymbols |
getDecimalFormatSymbols()
Returns the decimal format symbols for the current language
|
static DecimalFormatSymbols |
getDecimalFormatSymbols(String lang)
Returns the decimal format symbols for the given language
|
static String |
getDefaultLanguage()
Returns the two-letter code of the default language.
|
static String |
getFallbackLanguage()
Returns the two-letter code of the fall back language.
|
static Optional<String> |
getIfExists(String property,
String lang)
Returns a translated text for the given property in the given language
or null if no translation was found.
|
static DecimalFormatSymbols |
getMachineFormatSymbols()
Creates a new decimal format symbols instance which is independent of the current language or locale and
constantly set to use '.' as decimal separator with no grouping separator.
|
static String |
getMonthName(int month)
Returns the name of the given month in the current language
|
static String |
getMonthNameShort(int month)
Returns a three letter abbreviation of the name of the given month, like "Jan".
|
static String |
getMonthNameShort(int month,
String symbol)
Returns a three letter abbreviation of the name of the given month, like "Jan".
|
static DateTimeFormatter |
getShortDateFormat(String lang)
Returns the short date format (two digit year like 24.10.14) for the given language.
|
static Set<String> |
getSupportedLanguages()
Returns a list of two-letter codes enumerating all supported languages.
|
static String |
getSystemLanguage()
Returns the two-letter code of the system language.
|
static DateTimeFormatter |
getTimeFormat(String lang)
Returns the time format (without seconds) for the given language.
|
static DateTimeFormatter |
getTimeFormatWithSeconds(String lang)
Returns the full time format (with seconds) for the given language.
|
static DateTimeFormatter |
getTimeParseFormat(String lang)
Returns the time format which is intended to parse time value in the given language.
|
static Babelfish |
getTranslationEngine()
Provides direct access to the translation engine to supply new properties or inspect current ones.
|
static void |
init(Classpath classpath)
Initializes the engine based on the given classpath
|
static boolean |
isSupportedLanguage(String twoLetterLanguageCode)
Determines if the given language code is supported or not.
|
static String |
makeLang(String lang)
Checks if the given language is supproted.
|
static String |
nonNLS(String s)
Marks a string as deliberately not translated.
|
static <V> V |
parseMachineString(Class<V> clazz,
String value)
Parses the given string by expecting a machine independent format.
|
static <V> V |
parseUserString(Class<V> clazz,
String string)
Parses the given string by expecting a format as defined by the current language.
|
static <V> V |
parseUserString(Class<V> clazz,
String value,
String lang)
Parses the given string by expecting a format as defined by the given language.
|
static String |
safeGet(String property,
String fallback)
Returns a translated text for the given property or for the given fallback, if no translation
for property was found.
|
static String |
safeGet(String property,
String fallback,
String lang)
Returns a translated text for the given property or for the given fallback, if no translation
for property was found.
|
static void |
setDefaultLanguage(String lang)
Overrides the default language as defined in the configuration (
nls.defaultLanguage ). |
static String |
smartRound(double number)
Outputs integer numbers without decimals, but fractional numbers with two digits.
|
static void |
startMonitoring(Classpath classpath)
Start the monitoring of resources in development environments.
|
static String |
toMachineString(Object data)
Formats the given data in a language independent format.
|
static String |
toSpokenDate(Temporal date)
Converts dates to a "human" (e.g.
|
static String |
toUserString(Object object)
Formats the given data according to the format rules of the current language
|
static String |
toUserString(Object data,
String lang)
Formats the given data according to the format rules of the given language
|
@Nonnull public static String getCurrentLang()
CallContext.getLang()
@Nonnull public static String getDefaultLanguage()
nls.defaultLanguage
If this is set to "auto" the default language will be the system language.
@Nonnull public static String getFallbackLanguage()
CallContext
. If the value is
empty, getDefaultLanguage()
is returned.public static void setDefaultLanguage(String lang)
nls.defaultLanguage
).
This can be used to enforce the system language. However, setting nls.defaultLanguage to 'auto' is the recommended approach.
lang
- the two letter language code to use as default language.getSystemLanguage()
@Nullable public static String getSystemLanguage()
By default, SIRIUS initializes with the language set in nls.defaultLanguage
so a switchover
to the system language has to be performed manually.
nls.languages
), null will be returned as
CallContext.setLang(String)
doesn't change the current language if null is
passed in.public static Set<String> getSupportedLanguages()
nls.languages
public static boolean isSupportedLanguage(String twoLetterLanguageCode)
twoLetterLanguageCode
- the language as two-letter code@Nullable public static String makeLang(@Nullable String lang)
Note that if the given lang is empty or null, this method will also return null as a call
to CallContext.setLang(String)
with null as parameter won't change
the language at all.
lang
- the language to checkpublic static void init(Classpath classpath)
classpath
- the classpath used to discover all .properties filespublic static void startMonitoring(Classpath classpath)
classpath
- the classpath used to resolve .properties filespublic static Babelfish getTranslationEngine()
public static String get(@Nonnull String property)
If no translation is found, the translation for the default language is used. If still no translation is found, the property itself is returned.
property
- the key for which a translation is requestedpublic static String get(@Nonnull String property, @Nullable String lang)
The same fallback rules as for get(String)
apply.
property
- the key for which a translation is requestedlang
- a two-letter language code for which the translation is requestedpublic static String get(@Nonnull String property, int numeric)
property
- the property to fetchnumeric
- the numeric used to determine which version to useget(String, int, String)
public static String get(@Nonnull String property, int numeric, @Nullable String lang)
The property has to be defined like:
property.key=Version for 0|Version for 1|Version for many
Alternatively, only two versions can be given:
property.key=Version for 1|Version for 0 or many
Based on the given numeric the right version will be chosen.
property
- the property to fetchnumeric
- the numeric used to determine which version to uselang
- the language to translate forpublic static Optional<String> getIfExists(@Nonnull String property, @Nullable String lang)
The same fallback rules as for get(String, String)
apply. However, if no translation
property
- the key for which a translation is requestedlang
- a two-letter language code for which the translation is requestedpublic static String safeGet(@Nonnull String property, @Nonnull String fallback, @Nullable String lang)
property
- the primary key for which a translation is requestedfallback
- the fallback key for which a translation is requestedlang
- a two-letter language code for which the translation is requestedpublic static String safeGet(@Nonnull String property, @Nonnull String fallback)
property
- the primary key for which a translation is requestedfallback
- the fallback key for which a translation is requestedpublic static Formatter fmtr(@Nonnull String property)
property
- the property to used to retrieve a translated patternpublic static Formatter fmtr(@Nonnull String property, @Nullable String lang)
property
- the property to used to retrieve a translated patternlang
- a two-letter language code for which the translation is requestedpublic static String nonNLS(String s)
Can be used to signal that a string needs no internationalization as it is only used on rare cases etc.
s
- the text which will be used as outputpublic static String getDayOfWeek(int day)
Calendar.Monday...Calendar.Sunday
) into textual their representation.day
- the weekday to be translated. Use constants Calendar.MONDAY
etc.""
if an invalid index was givenpublic static String getDayOfWeekShort(int day)
"Mo"
.day
- the weekday to be translated. Use constants Calendar.MONDAY
etc.""
if the given index was invalid.public static String getMonthName(int month)
month
- the month which name is requested (1..12)public static String getMonthNameShort(int month)
month
- the month to be translated (January is 1, December is 12).public static String getMonthNameShort(int month, String symbol)
month
- the month to be translated (January is 1, December is 12).symbol
- the symbol to append in case of abbreviationpublic static DateTimeFormatter getDateFormat(String lang)
lang
- the language for which the format is requestedpublic static DateTimeFormatter getShortDateFormat(String lang)
lang
- the language for which the format is requestedpublic static DateTimeFormatter getTimeFormatWithSeconds(String lang)
This should be used to format dates (times). Use getTimeParseFormat(String)
to parse strings
as it is more reluctant (or use parseUserString(Class, String)
).
The pattern in this case will conform to the PHP 5 patterns as these are used by some JavaScript libraries like jQuery timepicker. (See http://php.net/manual/en/function.date.php).
lang
- the language for which the format is requestedpublic static DateTimeFormatter getTimeFormat(String lang)
This should be used to format dates (times). Use getTimeParseFormat(String)
to parse strings
as it is more reluctant (or use parseUserString(Class, String)
).
lang
- the language for which the format is requestedpublic static DateTimeFormatter getTimeParseFormat(String lang)
In contrast to getTimeFormat(String)
and getTimeFormatWithSeconds(String)
this is used to parse dates and is more reluctant when it comes to formatting (parses '9:00' whereas
getTimeFormat(String) only accepts '09:00').
lang
- the language for which the format is requestedpublic static DateTimeFormatter getDateTimeFormat(String lang)
lang
- the language for which the format is requestedpublic static NumberFormat getDecimalFormat()
public static NumberFormat getDecimalFormat(String lang)
lang
- the language for which the format is requestedpublic static DecimalFormatSymbols getDecimalFormatSymbols()
public static DecimalFormatSymbols getDecimalFormatSymbols(String lang)
lang
- the two-letter code of the language for which the decimal format symbols should be returnedpublic static DecimalFormatSymbols getMachineFormatSymbols()
This is commonly used to exchange numbers between machines.
public static String toMachineString(Object data)
data
- the input data which should be converted to stringparseMachineString(Class, String)
independently of the language settingspublic static String toUserString(Object object)
object
- the object to be converted to a stringpublic static String toUserString(Object data, String lang)
data
- the object to be converted to a stringlang
- a two-letter language code for which the translation is requestedpublic static String toSpokenDate(Temporal date)
The following texts are supported:
date
- the date to be formattedpublic static <V> V parseMachineString(Class<V> clazz, String value)
This can parse all strings generated by toMachineString
V
- the target type be be parsedclazz
- the expected class of the value to be parsedvalue
- the string to be parsedIllegalArgumentException
- if the given input was not well formed or if instances of clazz
cannot be created. The thrown exception has a translated error message which
can be directly presented to the user.public static <V> V parseUserString(Class<V> clazz, String value, String lang)
V
- the target type be be parsedclazz
- the expected class of the value to be parsedvalue
- the string to be parsedlang
- the two-letter code of the language which format should be usedIllegalArgumentException
- if the given input was not well formed or if instances of clazz
cannot be created. The thrown exception has a translated error message which
can be directly presented to the user.public static <V> V parseUserString(Class<V> clazz, String string)
V
- the target type be be parsedclazz
- the expected class of the value to be parsedstring
- the string to be parsedIllegalArgumentException
- if the given input was not well formed or if instances of clazz
cannot be created. The thrown exception has a translated error message which
can be directly presented to the user.public static String convertDuration(long duration, boolean includeSeconds, boolean includeMillis)
duration
- the duration in millisecondsincludeSeconds
- determines whether to include seconds or to ignore everything below minutesincludeMillis
- determines whether to include milli seconds or to ignore everything below secondspublic static String convertDuration(long duration)
This is a boilerplate method for convertDuration(long, boolean, boolean)
with
includeSeconds and includeMillis set to true.
duration
- the duration in millisecondspublic static String smartRound(double number)
Discards fractional parts which absolute value is less or equal to 0.00001
.
number
- the number to be roundedpublic static String formatSize(long size)
Supports sizes up to petabyte. Uses conventional SI-prefixed abbreviations like kB, MB.
size
- the size to format in bytesCopyright © 2018. All rights reserved.