Approaches for Multi Language ActionScript 3 Websites

How best to approach setting up a Flash based website to handle?Ǭ†multiple?Ǭ†languages is often a topic of discussion at the various Flash forums I frequent.

Adobe have a few pointers for creating multi language Flash projects, using the Strings Panel. This looks OK, but I’ve never actually used it, so cant really comment beyond reading over the documentation.

My approach to mulitlanguage files consists of creating a seperate XML file for each language I want to use in the application, and then loading that file at load time where a user can select it before the site loads and allowing for the user to change the language file at runtime, after the site has loaded and all content is displayed. If a user selects a language file then that selection is stored as a SharedObject, so that with subsequent site loads, the language file is automatically selected and loaded.

Setting up a Flash project for multiple language selection is fairly painless. I’ll run through a general approach to setting up your projects, and then a couple of ActionScript implementations that explore different means of achieving the same result; namely load and runtime selection of language files. One note, is that these example files are pure ActionScript projects, all content is dynamically created. However, if you have static content, the general principles will still apply.

The first thing you will want to set up is your XML language files. I’ve created two, an English and a French version. (The French version is just some google translated English text, so excuse the probably massive translation errors :) ). The english.xml file is below.

<?xml version="1.0" encoding="UTF-8" ?>
<!--  English XML File - 2008 -->
<website lang="english">
 
<ui>
<lang id="changelang" title="Change Language" />
<noponies id="blog" title="View Blog Entry" url="http://www.blog.noponies.com/feed" />
<noponies id="illustrations" title="View Illustrations" url="http://www.noponies.com/" />
<noponies id="about" title="About" url="http://www.blog.noponies.com/about" />
</ui>
 
<!-- DUMMY --> 
<people>
<person id="people/director" title="Dale Sattler" > 
<text><![CDATA[<p><strong>About</strong>
 
I?¢‚Ǩ‚Ñ¢m a New Zealand based Designer and Illustrator. Apart from what you see here I like to surf, draw and enjoy the outdoors.
 
This site is little blog to place the Flash and other technical stuff that does not fit into my main http://www.noponies.com site, which is more focused on my illustration and design.
 
Drop me a line if you find the files useful and maybe even send me a link to the site in which it was used. Feel free to make a donation to my paypal account if you want to reward me for the effort I put into making this resource. Its not a requirement, but is nice. </p>]]>
</text>	
</person>
</people>
</website>

As you can see, the XML is wrapped in an website tag with an attribute for the language, called “lang”. We then have a ui and a people tag, with various children. The ui tag, will provide our sites interface with its text labels, and the people tag will simply provide us with some dummy text content.

The most important element to pick up from the XML file, is the use of the id attribute for each XML fragment that corresponds to some actual content in our Flash website. This id attribute must be unique for each element and is used by Flash a key when we search through each XML file, matching the id attribute against each of our Flash sites elements own id tags. This key based approach is also the means by which I create Flash sites that use SWFAddress. Each XML element also has either a title or a text tag which corresponds to the Flash elements actual content. If you contrast the english.xml against the french.xml, you will see that the id attributes are unchanged, it is only the title or text elements that change.

So, that is the XML part of the project explained. Now, onto the Flash implementations. As I mentioned earlier, there are various different approaches to using multiple language files in Flash. As a Flash developer, you have a few options to consider. The first is when do you want a user select their language file. Generally, you would want a user to make this selection before the site has loaded, a sort of “Choose your language” splash page. This is the most simple approach, as it negates the need to allow for users changing language settings once the website has loaded and its content has displayed. The second is to allow for runtime language changes, although hardly any users would want to change their language after the site has loaded, but there may be a small subset that perhaps make a mistake etc. This approach is a little more complicated, because you need to build into your project the means to let all the elements that can change their language, that they have to change their language, and then also a means for passing around the relevant language data to each of these elements.

Which ever way you approach your language swapping, your interface will need to be flexible enough to accommodate the various different amounts of text it takes to express something like “About Us” in different languages.

For this demo I have allowed for runtime language changing, with a default language “english” loading. I have not added in any functionality to storing a language selection, but this is simple to add.

Ok, onto the ActionScript.

As I mentioned earlier, the main problem to overcome with runtime language swapping is how to notify all the interested parties in Flash that they need to update their language and dimensions when a user chooses to change language settings. There are various means to approach this, but what it boils down to, is a means to store the language XML in a way that allows for all elements to access it, and a means for notifying all elements that they must change their language file and probably their dimensions.

I’ve implemented 3 different approaches to changing language files in Flash. Each example is a different means for solving the problem of passing the language files XML to each element and notifying them that they need to change their language settings, along with the means to actually change language files. The different approaches are; Singleton Pattern, Custom Event with params and Observer Pattern. Universal to each example is the use of a simple interface that each ui element implements to guarantee the presence of a setLanguage method in all ui elements that need to change their language. I probably didn’t really need an interface for this one method, but with a complicated site, with many ui elements, it can be easy to forget a particular method.

1 – Singleton Using a Singleton to store language file in a globally accessible point. Singleton classes, allow for what many consider a bit of a hack, and that is global access to one class instance. In this implementation, the Singleton is used as an XML loading class, with each class that requires access to the language file importing a reference to this Singletons one and only instance. When a user chooses to change language, an event is dispatched, that all elements are listening for. When this notification event is received, each subscribing item then queries the Singleton for the sites language file, parses this XML data matching its id against the XML elements id code, if a match is found, then that XML fragment is used by the element for its language setting, and the ui updates. This approach works fine, but I don’t like the long …. lookups on the Singleton instance and the main deal breaker for me with this implementation is that it locks the site as a whole into accessing this one point for its data. So, if we wanted to provide the language file by some means other than via an external xml file, then we would have some issues.

2 – Custom Event Using a custom event with a parameter that allows for us to pass the XML data around the site is the obvious choice I think for sites that need to change language files. The implementation is simple, write a custom event class that allows for us to pass around the XML data from the language file. Have each element that needs to change its language listen for this event, and when a user changes language, simply dispatch the event. When this notification event is received, each subscribing item then queries the events parameter for the sites language file, parses this XML data matching its id against the XML elements id code, if a match is found, then that XML fragment is used by the element for its language setting, and the ui updates. I like this approach, as it decouples the sites ui elements from one particular source for that XML data, it could come from a database, or an external xml file. Either way, the ui elements have no idea where the data comes from, they just listen for an event.

3 – Observer Design Pattern Using an Observer pattern works fairly well in this situation. Again, the implementation is fairly straightforward, we create an Observer class, which has the usual addSubscriber, removeSubscriber and update methods. Each ui element then subscribes to the observer pattern instance, and when a user changes language files, the observer then loops through its subscribed children, calling each child’s setLanguage method, passing the sites new XML language data through to each child as a argument of the setLanguage method. This approach works well I think. It still maintains a loose relationship between the ui elements and the language data. The only thing I don’t really like about this approach, and this design pattern is that it feels like I’m replicating the basic event dispatcher model in Flash, and so feels redundant. Maybe I’m missing something. One thing to note, is that I changed the interface for this approach to allow for the setLanguage method to accept XML as an argument.

So, whats the best approach? I tend to prefer using custom events, mostly out of force of habit. I’m curious to see how other people approach swapping language files in Flash website projects?

View Example
You can view a pretty simple demo of the event based approach here: http://www.noponies.com/dev/as3_lang/

Source Files
Files for each of the 3 approaches are here: Language Swapping Source Files (877) - 2.3 MB

Each example is well commented, and the comments should explain most of the functionality.

ActionScript 3 Flexible Layout Class

ActionScript 3 NpFlexLayout Class

The NpFlexLayout Class is designed to simplify aligning DisplayObjects to stage dimensions and responding to changes in stage dimensions at run time by a user or between different users.

This supersedes a simpler implementation of this approach, called “ActionScript 3 Liquid Layout Manager”, which I’ll leave on the site.

As opposed to the LayoutManager class, the NpFlexLayout class is an actual layout manager, taking care of instantiating, destroying and passing on requests to instances of an internal class called NpFLexLayoutItem which wraps each displayObject passed to the NpFlexLayout Class.

Class Features

  • Position objects (x,y) on the stage as a proportion of the stages dimensions
  • offSet each displayObject (x,y) from where it would be by an absolute pixel value
  • set objects to stretch to fit either stage width / height or a proportion of stage dimensions
  • per object minX & minY settings where reflowing, resizing will cease
  • use either the clips registration point, or its visual center (width/height)
  • pause, resume displayObjects responding to resize events
  • update individual class properties at runtime
  • tween objects properties at runtime (uses internal simple tween method)
  • set tween easing
  • add, remove displayObjects from class

View Example
Here is an example; http://www.noponies.com/dev/as3_flexlayout/

Source Files
Here is the relevant source file; Flex Layout Files (1224) - 127.6 KB
By downloading, you agree to the terms of use, outlined here: http://www.blog.noponies.com/terms-and-conditions

Dependencies
None.

Version 2 Beta
Ok, here is a beta of version 2 of this class. Have a play and let me know of any issues.

I’ve updated the beta with an constrain resize property. Let me know how it works out in testing etc

http://www.blog.noponies.com/wp-content/uploads/NpFlexLayout-v2-Beta.zip

ActionScript 3 Full Browser Background Content

ActionScript 3 Full Browser Background Content.

This is the second third iteration of this class. I decided to put the NpFSObjectResize Class to use within this demo, using that class as the superclass for here. As a result, this new version of this file is much more capable, supporting stage alignment modes along with a simple method for loading in new background images or swfs. You could use that feature to create a simple full browser slideshow, but be warned that I have not used the same bitmapData memory management techniques as I have within the Full Browser Cross Fading demo. So, your memory useage could get higher using this class as a slideshow (as you will be waiting on the garbage collector to do its thing).

This class has been completely rewritten, as of version 2. The api has changed, and many new methods and properties are present. The classes event model has been shifted into its own class, and a new class now wraps each display object, which is the foundation for more work on transitions other than a straight alpha fade. This version is not compatible with any older versions!

The demo file contains an example of using this class with an XML iterator, with various methods for creating slideshows etc. Along with this example, is a very simple implementation, commented out in the actionscript layer of the .fla.

Class Features

  • Stage Alignment Modes
  • Load New Bg Method
  • Specify Cross Fading Time
  • Specify Min Stage Scale points
  • Pass Through Mode, where scaling is disabled
  • Specify Max scale of content
  • Specify content only scales upwards

View Example
Here is an example. In this example, the first file (swf) is set to scale to only 70% of the stages size, and stay centered, the second file (bitmap) is set to scale to full browser dimensions. View example here: http://www.noponies.com/dev/as3_fullbrowser_bg/

Updates
04 August, 2008: Added in ability to load in swfs as content.
17 Oct, 2008: Method change, shift from using timer to crossfade to internal Adobe tween class.
06 Jan, 2009: Completely rewritten, new API, new classes, new event model.
08 July, 2009: Fixed the bug with short tween in times, and a small issue with garbage collection
13 July, 2009: Fixed a slight bug with the example files related to iterating in reverse over a collection

Source Files
Here is the relevant source file; Full Browser Content Files (2445)

I’ve been playing around with javascript libraries more and more recently, I converted this file into a lightweight jQuery plugin, which can be viewed here; http://www.noponies.com/dev/as3_fullbrowser_bg/resize.html. If there are no problems, I’ll release it. In many ways, the writing is kinda on the wall for Flash, javascript can do a lot of the presentation things I would normally do in Flash.

By downloading, you agree to the terms of use, outlined here: http://www.blog.noponies.com/terms-and-conditions

Dependencies
None.

ActionScript 3 Accordion Content

ActionScript 3 Vertical & Horizontal Accordion Menu with support for columns of content.

This example extends and supersedes an earlier version of this file I wrote about a year ago. I always wanted to make a horizontal version and extend the class, making it more flexible and useful to people.

As was the previous menu, this menu is a Class based ActionScript 3 Accordion Menu, with either a rollOver/RollOff activation scheme for a onRelease activation scheme. I also added in a better extended event class which allows you to control, or respond to all menu activity via events.

Class Features

  • Vertical or Horizontal Menu Orientation
  • Visible or invisible Menu Bar
  • Supports parsing functions passed to each menu item
  • Custom Menu Bar Assets
  • Option for persistent Menu Panels
  • Menu Spacing
  • Menu text position
  • MenuBar colour / RollOver/Off colour
  • Menu text format
  • Menu tween speed
  • Menu easing type
  • MenuBar width, height, visibility
  • Menu Click behaviour
  • Disable / Enable Menu
  • Open Particular Panels
  • Menu attributes are applicable on a per menu item basis
  • etc, etc.

View Example
http://noponies.com/dev/as3_accordion/

Source Files
Donator only file!
If you want a less capable version of this file, you can download this; http://www.blog.noponies.com/archives/39

Dependencies
TweenLite – http://www.tweenlite.com

ActionScript 3 FullScreen Object Resize Class

The NpFSObjectResize Class is a fairly simple ActionScript 3 class for displaying any DisplayObject at fullScreen (fullBrowser) screen dimensions. The class gives you the options of resizing to full the entire screen, or to resize to either width or height. You are also able to align your objects anywhere within the stages dimensions. This allows for you control where your objects scale from (via positioning) them. So, you can center your objects, or align them bottom right and so on.

Updates 1 August 2008: Updated with new stage alignment properties, adjustments to constructor.

Class Features

  • Min Stage Width – point at which scaling will turn off
  • Min Stage Height – point at which scaling will turn off
  • Use Stage Min – Controls whether or not objects scale or clip at a the MIN stage points
  • Scale Modes – “full”, “height”, “width”
  • Align Objects within stage dimensions
  • Position Objects within stage dimensions

View Example
http://www.noponies.com/dev/as3_fsresize/

Source Files
Here is the relevant source file: FullScreen Resize Object Files (1705) - 348.14 KB
By downloading, you agree to the terms of use, outlined here: http://www.blog.noponies.com/terms-and-conditions

Dependencies
None

Updates
03 October, 2008: Updated main resize code, as there was a slight bug when resizing

Dependencies
None!

Beta Version

I’ve created a new version of this class, that includes mouse panning for objects along with a panning to a point method

You can get the beta from here. NpFsObjectResize Beta (51) - 482.74 KB
Please let me know of any errors, or issues with the code.

ActionScript 3 Contextual Menu Manager Class

The NpContextMenu Class is a fairly simple ActionScript 3 class for creating and interacting with Contextual Menus.

The class simply redispatches the ContextMenuEvent.MENU_ITEM_SELECT event to any parties interested in responding to menu events outside of the actual class. This allows for a little more flexibility with how events are handled and how the menu itself is set up. By using this approach the menu can be a a little more dynamic.

Class Features

  • Public Methods
  • addMenuItem() – add menu item at run time
  • removeMenuItem() – remove menu item at run time
  • hideMenuItem() – hide menu item at run time
  • showMenuItem()_ – show menu item at run time
  • + a few getters

View Example
http://www.noponies.com/dev/as3_context_menu/

Source Files
Context Menu Class Source File (708) - 96.02 KB

Dependencies
None

Simple ActionScript 3 Slideshow Engine

Simple ActionScript 3 Slideshow engine. This class file simply creates a slideshow that in its current setup, cross fades between each image. The class uses external images and is in fact, abstracted from sourcing any images. It is up to you to provide the class with an array of image URLs for it to use. This is a reworking of an earlier class, which has now been removed from this site.

Class currently uses tweenLite to handle the alpha transitions. However, this is simply replaced via a simple timer function. But I’ve left tweenLite in the class so that people can change the transitions to other types, like, blur, photoFlash etc easily by changing the tween type. If you just want an alpha fade and don’t use tweenLite elsewhere in your project, replace the tweenLite functionality with your own alpha fading transition. An example of which, is commented out in the class source code.

Class settings are instance based, which you access via getters/setters. So you can have muliple slideshow instances running at the same time

Class Features

  • External Images
  • Control over image cross fade time
  • Control over image display time
  • Linear or random image progression
  • Automatic (timer) progression, or via mouse event
  • Looping or play once
  • Pause / Resume the slideshow in auto mode

Via setters, you can access and set the following properties for each slideshow instance..

xFadeTime: get/set – DataType:uint – Sets crossfade time or transition time between two slides in seconds
imageTime: get/set – DataType:uint – Sets total image display time, not including xFadeTime in seconds
slidesPlayRandom: get/set – DataType:Boolean – Sets the linear or random play order for slides. Set to true for random playback.
slidesLoop: get/set – DataType:Boolean – Sets whether the slideshow loops, or stops at end of images array. True = looping
slidesAutoPlay: get/set – DataType:Boolean – Sets whether the slides use the timer to auto progress, or whether a user must click on the slides to progress. True = autoPlay

Look in the demo .fla’s document class for examples of listening for the classes various events. You can listen for Open, Progress, Complete and SlideShowComplete events at the class instance level. You can also dispatch a pause and a resume event to the class. Which will pause / resume the class if it is in automatic mode.

View Example
http://www.noponies.com/dev/as3_xslideshow/

Source Files
http://www.blog.noponies.com/wp-content/uploads/as3_xslideshow_1_2.zip

Dependencies
tweenLite

« Previous PageNext Page »