Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-1870] Android: support several localization enhancements

GitHub Issuen/a
TypeNew Feature
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2011-04-17T01:57:24.000+0000
Affected Version/sn/a
Fix Version/sRelease 1.5.0 M03
ComponentsAndroid
Labelsandroid, feature, rplist
ReporterBill Dawson
AssigneeBill Dawson
Created2011-04-15T03:04:22.000+0000
Updated2011-04-17T01:57:24.000+0000

Description

Add to Ti.Locale:

getCurrentLanguage() - returns language string such as "en".

getCurrentCountry() - returns "US" for example.

getCurrentLocale() - returns "en-US" for example.

getCurrencyCode(locale_string) - e.g., pass "en-US" and it will return "USD"

getCurrencySymbol(currency_code) - e.g., pass "USD" and it will return "$"

getLocaleCurrencySymbol(locale_string) - e.g., pass "en-US" and it will return "$"

Locale and format string support for String.formatDecimal:

String.formatDecimal(2.5) - returns 2.5 formatted in current locale. If a european locale, for example, will return "2,5".

String.formatDecimal(2.5, "000.000") - e.g., returns "002.500" if current locale is en_US, or "002,500" if current locale is de_AT.

String.formatDecimal(2.5, 'de-AT') - returns "2,5"

String.formatDecimal(2.5, 'de-AT', '000.0000') - returns "002,50000"

You can exercise these with this simple program (app.js):

/*global Ti, Titanium, alert, JSON */
Titanium.UI.setBackgroundColor('#000');
var win = Titanium.UI.createWindow({  
    title:'Test',
    backgroundColor:'#fff',
    fullscreen: true,
    exitOnClose: true
});

var rows = [];
function makeRow(title) {
    rows.push(Ti.UI.createTableViewRow({title: title}));
}

var statements = 
    ['Ti.Locale.getCurrentLanguage()',
     'Ti.Locale.getCurrentCountry()',
     'Ti.Locale.getCurrentLocale()',
     'Ti.Locale.getCurrencyCode(Ti.Locale.getCurrentLocale())',
     'Ti.Locale.getCurrencyCode("en-GB")',
     'Ti.Locale.getCurrencyCode("-GB")',
     'Ti.Locale.getCurrencySymbol("EUR")',
     'Ti.Locale.getCurrencySymbol("GBP")',
     'Ti.Locale.getLocaleCurrencySymbol("en-US")',
     'String.formatDecimal(2.5)',
     'String.formatDecimal(2.5, "000.00")',
     'String.formatDecimal(2.5, "de-DE")',
     'String.formatDecimal(2.5, "de-DE", "000.00")'
     ];
                  
for (var i = 0; i < statements.length; i++) {
    makeRow(statements[i] + " = " + eval(statements[i]));
}

win.add(Ti.UI.createTableView({data:rows}));

win.open();

Comments

  1. Bill Dawson 2011-04-15

    (from [11d5b5f2c891cbd6f3bd6620fc59e4db93ee7815]) [#1870] More localization support including currency info and arguments to String.formatDecimal http://github.com/appcelerator/titanium_mobile/commit/11d5b5f2c891cbd6f3bd6620fc59e4db93ee7815"> http://github.com/appcelerator/titanium_mobile/commit/11d5b5f2c891c...

  2. Bill Dawson 2011-04-15

    Todo:

    • update api docs

    • ticket for iOS guys

  3. Bill Dawson 2011-04-15

    Per #2333's directive to use dash instead of underscore, the examples above were changed. E.g., en_GB -> en-GB.

  4. Bill Dawson 2011-04-15

    (from [f39a6cc5486745894e7f08afe740cb418c74f967]) [#1870 state:fixed-in-qa] Document and unit test for Ti.Locale enhancements; fix one place where underscore and dash were swapped mistakenly. https://github.com/appcelerator/titanium_mobile/commit/f39a6cc5486745894e7f08afe740cb418c74f967"> https://github.com/appcelerator/titanium_mobile/commit/f39a6cc54867...

  5. Bill Dawson 2011-04-15

    (from [19a59fd7caded841d66bdaded43240332b90d76e]) [#1870] Add one more sentence to the Locale.tdoc https://github.com/appcelerator/titanium_mobile/commit/19a59fd7caded841d66bdaded43240332b90d76e"> https://github.com/appcelerator/titanium_mobile/commit/19a59fd7cade...

  6. Matt Schmulen 2011-04-15

    pass Android 1.6 Titanium SDK version: 1.5.0 (11/24/10 12:05 c0aff27)
    pass Android 2.1 Titanium SDK version: 1.5.0 (11/24/10 12:05 c0aff27)

JSON Source