Singleton class for Localization in Flex

Default way of using resourceManager

resourceManager.getString(bundleName,resourceName,parameters)

We have customized way of using resourceManager by using singleton class : ResourceUtil

Modified syntax of using ResourceUtil

ResourceUtil.string(resourceName,parameters)

— It reduced the code by not using bundleName each time. Find the code below to create singleton class

ResourceUtil.as

public class ResourceUtil
{
public static const DEFAULT_BUNDLE:String = ‘ResourceFileName’;

public static function string(key:String,parameters:Array=null):String
{
return ResourceLoader.instance().getString(DEFAULT_BUNDLE,key,parameters);
}
}

ResourceLoader.as :

package
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

import mx.resources.IResourceManager;
import mx.resources.ResourceManager;

[ResourceBundle(“ResourceFileName”)]
public class ResourceLoader extends EventDispatcher
{
public static const DEFAULT_LOCALE:String=’en_US’ ;
public static var _instance:ResourceLoader ;
private var _resourceManager:IResourceManager ;

public static function instance():ResourceLoader
{
if(_instance == null)
{
_instance = new ResourceLoader(PrivateSingletonEnforcer);
}

return _instance ;
}

public function ResourceLoader(singletonEnforcer:Class)
{
if(singletonEnforcer!=PrivateSingletonEnforcer)
{
throw new Error(‘ResourceLoader cannot be instantiated directly.’)
}
_resourceManager = ResourceManager.getInstance();
}

public function getString(bundleName:String,resourceName:String,parameters:Array = null):String
{
var value:String = _resourceManager.getString(bundleName,resourceName,parameters);
if(value == null)
return resourceName ;
else
return value ;
}

}
}

class PrivateSingletonEnforcer
{
}

Advertisements
  1. #1 by motocross videos on April 8, 2013 - 7:31 pm

    Hey excellent website! Does running a blog like this require a great deal
    of work? I have absolutely no understanding of programming but I was hoping to start my
    own blog soon. Anyway, if you have any suggestions or tips for new
    blog owners please share. I understand this is off subject
    however I simply had to ask. Appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: