Skip to content
Titanium SDK
Version13.1.1

You are reading 13.1.1. See this page in 13.4.1.

Titanium.Locale

moduleAndroidiPhoneiPadmacOSsince 1.5

Extends Titanium.ModuleTitanium.Proxy

The top level Locale module.

The Locale module works with localization files to which are generated during compilation into the operating system specific localization formats. The Locale module provides locale-specific strings which can be referenced at runtime. Additionally, the module contains a few methods and properties for querying device locale information.

The macro L can be used as an alias for the Titanium.Locale.getString method.

Properties 6

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

currentCountry

Stringread-only

Country of the app's current locale, as an ISO 2-letter code.

This property holds a value such as US or GB. See the ISO 3166-1 alpha-2 country codes section of wikipedia for reference.

currentLanguage

Stringread-only

Language of the app's current locale, as an ISO 2-letter code.

This property holds a value such as en or fr. See the ISO 639-1 section of wikipedia for reference.

currentLocale

Stringread-only

Current app locale, as a combination of ISO 2-letter language and country codes.

This property holds a value such as en-US or en-GB. See the ISO 639-1 and ISO 3166-1 alpha-2 sections of wikipedia for reference.

Methods 11

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Formats a telephone number according to the current system locale.

Parameters
ParametersTypeDescription
numberString

Phone number.

Returns String

Returns the ISO 3-letter currency code for the specified locale.

This method returns one of the ISO 4217 currency codes. For example, getCurrencyCode('en-US') returns USD. See the xe.com website Currency List section of the www.xe.com website for reference.

Parameters
ParametersTypeDescription
localeString

Locale, as a combination of ISO 2-letter language and country codes. For example, en-US or en-GB. See the ISO 639-1 and ISO 3166-1 alpha-2 sections of wikipedia for reference.

Returns String

Returns the currency symbol for the specified currency code.

This method returns one of the international currency symbols. For example, getCurrencySymbol('USD') returns $. See the Currency Symbols section of the www.xe.com website for reference.

Parameters
ParametersTypeDescription
currencyCodeString

Currency, as an ISO 3-letter code. For example, USD or GBP. See the Currency List section of the www.xe.com website for reference.

Returns String

Returns the currency symbol for the specified locale.

This method returns one of the international currency symbols. For example, getLocaleCurrencySymbol('en-US') returns $. See the Currency Symbols section of the www.xe.com website for reference.

Parameters
ParametersTypeDescription
localeString

Locale, as a combination of ISO 2-letter language and country codes. For example, en-US or en-GB. See the ISO 639-1 and ISO 3166-1 alpha-2 sections of wikipedia for reference.

Returns String

Returns a string, localized according to the current system locale using the appropriate /i18n/LANG/strings.xml localization file.

This method is functionally identical to its alias Global.L. For example, Ti.Locale.getString('thisKey', 'missing key') produces the same result as L('thisKey', 'missing key').

When using with Global.String.format to localize a phrase with multiple string substitution placeholders, add the formatted="false" directive to the relevant strings.xml element. See example for a demonstration.

String Localization Demonstration

Localize two strings, "hello" and "goodbye" (using the keys "greeting" and "signoff") for English and Spanish system locales.

Note: use the device's system settings to change locales and test this code.

/i18n/en/strings.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <string name="phrase" formatted="false">You say %2$s and I say %1$s!</string>
  <string name="greeting">hello</string>
  <string name="signoff">goodbye</string>
</resources>

/i18n/es/strings.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <string name="phrase" formatted="false">You say %2$s and I say %1$s!</string>
  <string name="greeting">hola</string>
  <string name="signoff">adios</string>
</resources>

/Resources/app.js

var i18nMissingMsg = '<no translation available>';

// for English locales, displays "You say goodbye and I say hello!"
// for Spanish locales, displays "You say adios and I say hola!"
Ti.API.info('You say ' + Ti.Locale.getString('signoff', i18nMissingMsg) + ' and I say ' + Ti.Locale.getString('greeting', i18nMissingMsg) + '!');

// uses alias L() instead of Ti.Locale.getString()
// formatted="false" is used in the strings.xml element for multiple string substitution
// for English locales, displays "You say goodbye and I say hello!"
// for Spanish locales, displays "You say adios and I say hola!"
Ti.API.info(String.format(L('phrase'), L('greeting', i18nMissingMsg), L('signoff', i18nMissingMsg)));
Parameters
ParametersTypeDescription
keyString

Key of string.

hint?String

Text to return if key does not exist in the localization file.

Returns String

Parses a number from the given string using the current or given locale.

Parsing Localized Numeric Strings

// You can convert to/from a localized numeric string like this.
var numericString = String.formatDecimal(1234.5);
var numericValue = Ti.Locale.parseDecimal(numericString);

// The numbers returned will be the same for the 2 different locales.
var number1 = Ti.Locale.parseDecimal('1,234,567.8', 'en-US');
var number2 = Ti.Locale.parseDecimal('1.234.567,8', 'de-DE');
Parameters
ParametersTypeDescription
textString

The string to be parsed.

locale?String

Locale, as a combination of ISO 2-letter language and country codes. For example, en-US or en-GB. See the ISO 639-1 and ISO 3166-1 alpha-2 sections of wikipedia for reference.

Returns Number

Returns the parsed number. Returns NaN (Not-a-Number) if given string is invalid.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Sets the current language of the application.

This property holds a value such as en or fr. See the ISO 639-1 section of wikipedia for reference.

On Android you can also specify combination of a ISO 2-letter language and country codes. For example, en-US or en-GB. See the ISO 639-1 and ISO 3166-1 alpha-2 sections of wikipedia for reference.

When changing to a language with a different layout direction, such as English to Arabic or vice-versa, you will need to close and re-open the window in order to apply the new right-to-left setting.

This will restart the app internally to properly apply the new language setting. If you want to avoid this behavior, you can listen for the change event and handle the language change manually.

When a custom language is set using this method, the SDK will persist this setting across app restarts.

Parameters
ParametersTypeDescription
languageString

The specified ISO 639-1 code.

Events 1

change

since 7.5.0

Fired when the device locale changes.

On Android, when a listener for this event is not present, by default the application will soft restart upon a locale change. Once a listener for this event is present, the behaviour for a locale change is developer defined.