Quick Tip: Simpliest dictionary

Here is a short extract from a very simple dictionary engine I use on few of my latest projects. Even though it is based on single static method replace() 🙂 , I always search and copy-paste the file from project to project, so I decided to publish it, maybe it can help someone else too …

package
{
    public class Dict
    {
        public static const LINK_IN_CLIPBOARD:String =
            "%1\nLink has been copied into the clipboard."
        
        public static const UPDATE_FLASH_PLAYER:String = 
            "Please update your flashplayer.";
        
        public static const HELLO:String = "%1 %2";

        public static function replace(text:String, ... rest):String
        {
            var i:uint = 1;
            for each(var repl:String in rest)
                text = text.replace("%" + i++, repl);
            return text;
        }
    }
}

Usage:

trace(Dict.UPDATE_FLASH_PLAYER);
trace(Dict.replace(Dict.LINK_IN_CLIPBOARD, "http://somelink"));
trace(Dict.replace(Dict.HELLO, "Hello", "World"));

Have a nice one.

2 comments so far

  1. IQpierce September 22, 2010 19:10

    Interesting approach. But calling this a “Dictionary” seems a bit misleading, since a Dictionary is often the term used for an Associative Array.

    I would call this something like a “TextGenerator” or something…

    It seems like this is really close to being useful for localization, but maybe not quite there?

  2. Jozef Chúťka September 22, 2010 22:23

    You are right IQpierce, maybe I named it wrong, correct me if I am wrong… this class usualy holds all the texts / messages in my applications, it helps me find and correct texts + it works with code completion + used with mxml binding… I like keeping things simple

Leave a comment

Please be polite and on topic. Your e-mail will never be published.