Author Archive

Using %tokens% with Flex resource bundles

Tuesday, December 8th, 2009

When I use Flex, I make extensive use of Resource Bundles for all but the most basic applications. In short, they allow you to keep all of your static string values (and non-string values) separated out of your code. That way, down the line it’s a very easy change when you want to localize the site in another language, rebrand it for another audience, or when a client asks you to change a button label or error message. Granted, this level of detail isn’t always necessary but I think it’s a good habit to get into even if it’s overkill for some projects.

But resource bundles can’t help you in every scenario. I often find myself needing to construct a sentence with live data inserted. For this, the resource strings are less helpful. For example, how would you write:

"At " + time + ", you received a message from " + userName + "."

With resource strings, you would have to do something like

at=At
message=, you received a message from
period=.

Which completely negates the whole point of using resource bundles. What to do?

Token replacement

tokens
What I really wanted was a way to inject data into the string. I’ve seen this done by use of tokens in other languages like Objective-C so I thought I’d give it a try. The result was surprisingly lightweight.

I created this utility that lets you use tokens (delimited by % signs) that can be replaced by live data when you retrieve the resource. Using this class, the example above would look more like this:

receivedMessage=At %time%, you received a message from %userName%.

// then in your code
trace(ResourceStringUtil.getResoureceStringWithTokens("receivedMessage", {time: "11:49", userName: "Mims"}));

// displays
At 11:49, you received a message from Mims.

Here’s the source code Feel free to use it and modify it for your purposes.

Using ant for Flex – a no B.S. guide – Part 4 of 4

Friday, November 13th, 2009

Now that you’ve been through 3 exhaustive tutorials on ant for flex, it’s time to use your knowledge you’ve gained to turn your back on the built-in Flex compiler and switch to ant for your builds. This part applies to FlexBuilder (and FlashBuilder) users only although it may apply if you’re using some other eclipse-based plugin. If you’re using another program like TextMate or Flash CSx, you can skip this article.
(more…)

Using ant for Flex – a no B.S. guide – Part 3 of 4

Friday, November 13th, 2009

This third installment talks about tips and tricks for using ant with version control systems.
(more…)

Using ant for Flex – a no B.S. guide – Part 2 of 4

Friday, November 13th, 2009

In this part, I get more nitty-gritty about working with flex’s ant tasks.
(more…)

Using ant for Flex – a no B.S. guide – Part 1 of 4

Friday, November 13th, 2009

About to burst with build targets

After spending hours wrangling with my ant build script and finally making everything work correctly, I am convinced of two things:

  1. ant is a very powerful tool for compiling code but it can also do so much more.
  2. The support for ant for flex online is abysmally frustrating.

So, I’d love to share with you some of the tips I’ve picked up after tweaking my script to the point where it finally worked!

Introducing ant

First of all, let me introduce the basics of what ant is. I couldn’t really say it better than the thousands of nameless editors of WikiPedia.

Apache Ant is a software tool for automating software build processes. It is similar to Make but is implemented using the Java language, requires the Java platform, and is best suited to building Java projects.

The most immediately noticeable difference between Ant and Make is that Ant uses XML to describe the build process and its dependencies, whereas Make has its Makefile format. By default the XML file is named build.xml.

Ant is an Apache project. It is open source software, and is released under the Apache Software License.

So in short, ant lets you use an XML based script to run a series of commands typically used to build software projects. It was originally designed to be “for Java, in Java” but it’s popularity and ease of use has created interest beyond the Java community. For flex, ant can be used for compiling ActionScript and Flex projects into SWFs, creating SWC files, generating documentation with ASDocs, and other actions associated with compiling.

There are a lot of great tutorials online that can cover the basics of how ant works so I’m not going to go into a general overview in this article. Instead, I’m going to try to focus on some of the practical aspects of getting a build set up when working with Flex. However, here are some links to the official ant documentation some helpful tutorials for the basics:

Ant Basics tutorial

Another basics tutorial (this time for Flex)

Official Flex documentations

Ant Folklore

After the jump, I’ll get into some of the ways that ant can be useful for Flash and Flex projects.
(more…)