Code Monkey home page Code Monkey logo

as3-localemanager's Introduction

AS3-LocaleManager

The AS3-LocaleManager provides basic localization support for ActionScript apps. It takes care of loading/parsing resource bundle files (*.txt) and returns localized resources based on the precedence of the locales set in localeChain.

For some reason, mx.flex.ResourceManager does not always work as expected [1]. I ran into this issue while working on a AS3 project for AIR 3.6 (using Flash Builder 4.7). This class is a quick workaround for the problem.

[1] http://forums.adobe.com/message/5182578

Resource files

The bundle files are stored in app://locale/[locale]/[bundleName].txt (ie.: app://locale/en_US/localizedStrings.txt). Make sure the directory "locale" is copied to your build package:

  • Put the directory locale in your src directory.
  • Or add the directory containing 'locale' to your Source Path (Flash Builder: Project > Properties > ActionScript Build Path > Source Path).

The content format for bundle files is KEY = Value followed by a line break. You can use the "=" character within the value, but not for keys or comments.

# Any line without the "equals" character is a comment. 
A leading # does not harm.
LABEL_FIRSTNAME = Firstname
LABEL_LASTNAME  = Lastname

The locales are returned by precedence given in localeChain. Mix-ins are supported. This allows to work with incomplete bundles (ie. separate bundles for different regions of the same language).

Usage Sample

Resource files

// File: de_DE/bundleName.txt
// Complete resource file
CURRENCY_SHORT = EUR
PRICE          = Preis
USRMSG_UNLOCK  = Gratulation {0}, du hast jetzt {1}!

// File: de_CH/bundleName.txt
// Incomplete resource file (used as mix-in)
CURRENCY_SHORT = CHF

The sample uses mix-ins and placeholders.

Initialize LocaleManager and set localeChain

I recommend to use the handy LocaleUtil to sort supported locales based on system preferences.

var locales:LocaleManager = new LocaleManager();
locales.localeChain = LocaleUtil.sortLanguagesByPreference(
    ["de_CH", "de_DE", en_US], Capabilities.languages, "en_US");

Adding required bundles

Use addRequiredBundles to add all required bundles. This is typically used on startup. The bundle files are loaded/parsed at runtime. Adding only the required bundles saves time. You can safely omit useLinebreak:true if you do not use line breaks (\n) in your resource file (cmp. next example).

locales.addRequiredBundles([
    {locale:"de_DE", bundleName:"bundleName", useLinebreak:true},
    {locale:"de_CH", bundleName:"bundleName", useLinebreak:true}
], onComplete);

// optional complete responder
function onComplete(success:Boolean):void
{
    if ( success ) trace("Required bundles successfully added.");
    else trace("Adding required bundles failed.");
}

If you work with a single resource bundle per locale and you do not need mix-ins, this will do the job:

locales.addRequiredBundles([
    {locale:locales.localeChain[0], bundleName:"bundleName"}
], onComplete);

// complete responder (s. above)

Adding additional bundles

With addBundle you can add bundles later. If you have an app with many scenes/levels and many resources in your bundles it might be better to split the resources into several bundles and just load the ones really needed (ie. when switching levels).

locales.addBundle("en_US", "bundleName", false, onComplete);

// optional complete responder
function onComplete(locale:String, bundleName:String, success:Boolean):void
{
    if ( success ) trace("Bundle " + locale + "/" + bundleName + " added.");
    else trace("Adding bundle " + locale + "/" + bundleName + " failed.");
}

Use locales.localeChain[0] as parameter if you just need to add a single bundle for the primary locale.

locales.addBundle(locales.localeChain[0], "anotherBundleName", false, onComplete);

// complete responder (s. above)

Retrieving resources

getString returns a given resource of a given bundle. Localized of course. The third sample shows how to use placeholders and parameters.

// given localeChain ["de_CH","de_DE"]
locales.getString("bundleName", "PRICE"); // Preis
locales.getString("bundleName", "CURRENCY_SHORT"); // CHF
locales.getString("bundleName", "USRMSG_UNLOCK", ["Superman", "Superkraft"]); // Gratulation Superman, du hast jetzt Superkraft!

Make it better

Suggestions, improvements, feedback and whatever is welcome! @rekomat

as3-localemanager's People

Contributors

halfjust avatar rekomat avatar tafh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

as3-localemanager's Issues

Bundle name typo

Lines 188 and 190 use "bundle" but I believe it should be "bundleName".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.