Yoann on flex

July 11, 2007

Internationalization in Flex

Filed under: Flex, i18, internationalization — yoyoann @ 3:18 pm

The flex site I am working on need internationalization. So of course I googled a bit and found some informations on how to use i18n in Flex. And the only really good answer I found was doing different builds using resources.

However I need i18n at runtime, it is something the client needs, and I don’t want the user to reload the whole flex app when he wishes to change language.

I googled a bit more and found a blog post that explained how to get this to work … But could understand anything :

http://jeff.mxdj.com/internationalizing_flex_2_apps_pt_2.htm

So I am doing here a doable tutorial :

  1. create a Flex project.
  2. create a locale directory at the flex root
  3. add -sp locale to your flex compile options (right click, properties, flex compiler with eclipse) to add it to classpath
  4. create a fra.properties and an eng.properties file in the locale directory
  5. in the fra.properties, add the line : hello=salut
  6. in the eng.properties, add the line : hello=hi
  7. now let’s get to the code

We create a singleton that will take care of all this.

package
{

import mx.resources.ResourceBundle;
import flash.events.Event; public class Localizator
{

public static const FRENCH:int = 1;
public static const ENGLISH:int = 2; protected static var myLocalizator:Localizator;

[Bindable]
private var cur:ResourceBundle;

[ResourceBundle("eng")]
private var rb_eng:ResourceBundle;
[ResourceBundle("fra")]
private var rb_fra:ResourceBundle;

private var _lang:int;

public function Localizator (language:int=ENGLISH){

selectLang( language );

}

private function selectLang( language:int ) : void{

this._lang = language;
switch ( language ) {
case ENGLISH :

this.cur = rb_eng;
break;

case FRENCH :

this.cur = rb_fra;
break;

default :

this._lang = Localizator.ENGLISH;
this.cur = rb_eng;
break;

}

}

public static function getInstance (language:int) : Localizator {

if( Localizator.myLocalizator == null ){

Localizator.myLocalizator = new Localizator(language);

}
return Localizator.myLocalizator;

}

[Bindable(event="langChange")]
public function getText( key:String ) : String {

return this.cur.getString( key );

}

/**
* Shortcut for getText
*/
[Bindable(event="langChange")]
public function gT( key:String ) : String {

return this.getText(key);

}

public function get lang () : int {

return this._lang;

}

public function set lang ( language:int ) : void {

if( this._lang != language ){

this.selectLang( language );
var e:Event = new Event(”langChange”);
this.dispatchEvent(e);

}

}

}

}

You can now create a file, and for example add a lable with text=”{Localizatior.getInstance().gT(’hello’)}” and a button that will change the language of the localizator.

This example is pretty dirty but adapt it to match your needs and you should have an easy way to have an international app.

July 6, 2007

Code behind, or not code behind ?

Filed under: Code behind, Flex — yoyoann @ 8:16 am

I was just reading a bunch of blogposts and I saw one about code behind which hooked me.

Code behind is a pattern consisting in separating the view and the logic. There is an official way to implement it in Flex, as advocated by Adobe : Using Code Behind.

A bunch of time ago, I created my own component using this pattern and really couldn’t see any use to this, but I kept the thing in mind thinking it could become useful sometimes. When I saw this article today, I thought “oh maybe I will be able to get the point in using this”, and I started reading… I learnt really few things in the post, but one comment had a link to aTink BlogPost where he advocated that Flex’ way of doing code behind is bad. I must admit I agree with some of his statements, most in fact, but I don’t love his method any much than the classic one using inheritance : it looks like the very classical include everybody has been using with javascript, and which clearly wasn’t very really easy to maintain.

So I think I will go on using nothing like partial classes and code behind, and the script tag along with data binding should be fine for me.

July 5, 2007

Flex treeNavigator

Filed under: Flex, TreeNavigator — yoyoann @ 2:48 pm

I am currently building a website with a pretty complex navigation structure, and I stumbled on one of the Flex limitations : the absence of a tree component to browse on a viewstack. So I started writing something, thinking it would be plainly easy. However I quickly understood it would be way more difficult than I first thought, especially because my experience in dealing with dataBinding, e4x, and treeNavigation was almost null …

So I spent a good while trying to get an almost working version, that was really really ugly. I decided to rewrite all ..

I spent another while coding the whole stuff again, with genericity and resusability in mind, and I admit I am pretty proud of the result. It is still dirty and should be revamped a lot, and many things are not working yet, but it is already almost usable.

Demo - View Source

Hello world!

Filed under: Uncategorized — yoyoann @ 1:27 pm

Hello on my Flex blog.

This blog doesn’t aim to let you know all about Flex, but I will explain what are the difficulties I encounter when trying to master Flex !

You will see I still need a long work to get a good flex level, but I am trying hard !

Blog at WordPress.com.