Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class L10NManager
- Object
- L10NManager
The localization manager allows adapting values for display in different locales thru parsing and formatting capabilities (similar to JavaSE’s DateFormat/NumberFormat). It also includes language/locale/currency related API’s similar to Locale/currency API’s from JavaSE.
The sample code below just lists the various capabilities of the API:
Form hi = new Form("L10N", new TableLayout(16, 2));
L10NManager l10n = L10NManager.getInstance();
hi.add("format(double)").add(l10n.format(11.11)).
add("format(int)").add(l10n.format(33)).
add("formatCurrency").add(l10n.formatCurrency(53.267)).
add("formatDateLongStyle").add(l10n.formatDateLongStyle(new Date())).
add("formatDateShortStyle").add(l10n.formatDateShortStyle(new Date())).
add("formatDateTime").add(l10n.formatDateTime(new Date())).
add("formatDateTimeMedium").add(l10n.formatDateTimeMedium(new Date())).
add("formatDateTimeShort").add(l10n.formatDateTimeShort(new Date())).
add("getCurrencySymbol").add(l10n.getCurrencySymbol()).
add("getLanguage").add(l10n.getLanguage()).
add("getLocale").add(l10n.getLocale()).
add("isRTLLocale").add("" + l10n.isRTLLocale()).
add("parseCurrency").add(l10n.formatCurrency(l10n.parseCurrency("33.77$"))).
add("parseDouble").add(l10n.format(l10n.parseDouble("34.35"))).
add("parseInt").add(l10n.format(l10n.parseInt("56"))).
add("parseLong").add("" + l10n.parseLong("4444444"));
hi.show();Currencies
getCurrencyCode() is the ISO 4217 code of the user’s currency, and
formatCurrency(double, String) formats an amount in any currency for the
user’s locale. Every port uses the platform’s currency conventions, so a
price in euros reads €12.50 in the US and 12,50 € in France.
Where the platform has none (a Linux image without locale data, a server)
the symbol leads and US English separators apply. Use the standard
java.util.Currency for the facts about a currency itself – its fraction
digits, numeric code and symbol – which are the same on every platform.
L10NManager l10n = L10NManager.getInstance();
String mine = l10n.getCurrencyCode(); // "USD" for a user in the US
String price = l10n.formatCurrency(1250, "JPY"); // "¥1,250"
int digits = Currency.getInstance("JPY").getDefaultFractionDigits(); // 0Constructors
protected L10NManager(String language, String locale) | Instances of this class should be received via the Display class |
Methods
public static L10NManager getInstance() | Convenience method that invokes Display.getLocalizationManager() |
public String getLanguage() | Returns the current locale language as an ISO 639 two letter code |
public void setLocale(String locale, String language) | Forces the locale/language |
public String format(int number) | Format an integer number for this locale |
public String format(double number) | Format a double number for this locale |
public String getShortMonthName(Date date) | Gets the short month name in the current locale for the given date. |
public String getLongMonthName(Date date) | Gets the long month name in the current locale for the given date. |
public String formatCurrency(double currency) | Format a currency value in the user’s currency (getCurrencyCode()). |
public String formatCurrency(double amount, String currencyCode) | Formats an amount of the given currency for this locale, with the currency’s own number of fraction digits (none for the yen). |
public String getCurrencySymbol() | Returns the currency symbol for this locale |
public String getCurrencySymbol(String currencyCode) | The symbol this locale writes for the given currency: $ for US dollars in the US, US$ elsewhere. |
public String getCurrencyCode() | The ISO 4217 code of the user’s currency, such as "USD": the one the platform reports, or by default the currency of the country in getLocale(). |
public String formatDateLongStyle(Date d) | Formats a date in a long form e.g. Sunday January 1st 2001 |
public String formatDateShortStyle(Date d) | Formats a date in a short form e.g. 1/1/2011 |
public String formatDateTime(Date d) | Formats a date and a time in a default form e.g. 1/1/2011 10:00AM |
public String formatDateTimeMedium(Date d) | Formats a date and a time in a default form e.g. 1/1/2011 10:00AM |
public String formatTime(Date d) | Formats the time in a default form e.g. 10:00AM |
public String formatDateTimeShort(Date d) | Formats a date and a time in a default form e.g. 1/1/2011 10:00AM |
public boolean isRTLLocale() | Indicates whether the language is a right to left language |
public String getLocale() | Determines the locale (location) as an ISO 3166 country code |
public String format(double number, int decimalPlaces) | Formats a number as a String with a fixed number of decimal places |
public double parseDouble(String localeFormattedDecimal) | Parses a double based on locale conventions |
public long parseLong(String localeFormattedLong) | Parses a long based on locale conventions |
public int parseInt(String localeFormattedInteger) | Parses an integer based on locale conventions |
public double parseCurrency(String amount) | Parses the currency value |
Inherited methods
Constructor details
L10NManager
protected L10NManager(String language, String locale)Method details
getInstance
public static L10NManager getInstance()Returns
getLanguage
public String getLanguage()Returns
setLocale
public void setLocale(String locale, String language)Parameters
localeString- the new locale
languageString- the language to use
format
public String format(int number)Parameters
numberint- the number to format
Returns
format
public String format(double number)Parameters
numberdouble- the number to format
Returns
getShortMonthName
public String getShortMonthName(Date date)Parameters
dateDate- The date.
Returns
getLongMonthName
public String getLongMonthName(Date date)Parameters
dateDate- The date.
Returns
formatCurrency
public String formatCurrency(double currency)getCurrencyCode()).Parameters
currencydouble- the monetary value
Returns
formatCurrency
public String formatCurrency(double amount, String currencyCode)Formats an amount of the given currency for this locale, with the currency’s own number of fraction digits (none for the yen).
The default implementation uses java.text.NumberFormat, which puts
the symbol first; the ports (Android, iOS, macOS, Windows, Linux, the
simulator, the browser) follow the locale’s separators and its
placement and spacing of the symbol.
Parameters
amountdouble- the monetary value
currencyCodeString- an ISO 4217 code such as
"EUR"
Returns
Throws
IllegalArgumentException- if
currencyCodeis not an ISO 4217 code
getCurrencySymbol
public String getCurrencySymbol()Returns
getCurrencySymbol
public String getCurrencySymbol(String currencyCode)$ for US
dollars in the US, US$ elsewhere.Parameters
currencyCodeString- an ISO 4217 code such as
"EUR"
Returns
Throws
IllegalArgumentException- if
currencyCodeis not an ISO 4217 code
getCurrencyCode
public String getCurrencyCode()"USD": the one the
platform reports, or by default the currency of the country in
getLocale().Returns
formatDateLongStyle
public String formatDateLongStyle(Date d)Parameters
dDate- the date
Returns
formatDateShortStyle
public String formatDateShortStyle(Date d)Parameters
dDate- the date
Returns
formatDateTime
public String formatDateTime(Date d)Parameters
dDate- the date
Returns
formatDateTimeMedium
public String formatDateTimeMedium(Date d)Parameters
dDate- the date
Returns
formatTime
public String formatTime(Date d)Parameters
dDate- the date time object
Returns
formatDateTimeShort
public String formatDateTimeShort(Date d)Parameters
dDate- the date
Returns
isRTLLocale
public boolean isRTLLocale()Returns
getLocale
public String getLocale()Returns
format
public String format(double number, int decimalPlaces)Parameters
numberdouble- the number
decimalPlacesint- decimals
Returns
parseDouble
public double parseDouble(String localeFormattedDecimal)Parameters
localeFormattedDecimalString- the locale formatted number
Returns
parseLong
public long parseLong(String localeFormattedLong)Parameters
localeFormattedLongString- the number
Returns
parseInt
public int parseInt(String localeFormattedInteger)Parameters
localeFormattedIntegerString- the number
Returns
parseCurrency
public double parseCurrency(String amount)Parameters
amountString- the amount