Search code examples
apache-flexlocale

FLEX - Set conditional locale strings in ActionScript


I'm trying to localize my Flex app, I have been able to setup the locale specifics and all the stuff inside MXML tags, that works pretty well, my question is, what about if I have for example:

if(loggin){
  loginBtn.label = "Logout";
}else{
  loginBtn.label = "Login";
}

How can I change with ActionScript those two strings to an other locale?

Thanks for any help!!


Solution

  • Use the ResourceManager

    if(loggin){
      loginBtn.label = resourceManager.getString(MyResourceBundles.LABELS,'login')
    }else{
      loginBtn.label = resourceManager.getString(MyResourceBundles.LABELS,'logout')
    }
    

    The downside of this approach is that bindings won't fire if the user changes language midway.

    Therefore, for this specific example, I'd reccommend states:

    <s:Button id="loginBtn" 
       label.loggedIn="{resourceManager.getString(MyResourceBundles.LABELS,'login')}" 
       label.loggedOut="{resourceManager.getString(MyResourceBundles.LABELS,'logout')}" />