Multilanguage in Actionscript 3 via Flash/Flex
4 minuti di lettura
I am here to share with you a solution to a problem that came out when I was coping the multi-language need for a flash web site.
I'll be exhaustive as well as concise to get the point. When you move from AS2 to AS3 and so to OOP you change definitely the way you use Flash forever. You now can use Flex to write down tons of classes and do everything with Flash. Since in Flex you can develop your stand-alone application, communication between Flash and Flex can be weird.
If you are here I guess you are developing a multi-language website in Flex yet you are not able to control the language at runtime from Flex to Flash and the other way around.
The example
--
Basically we have a dynamic text on Flash and want to change the language previously set in the string panel via Flex. Have a look to the screenshot up here. Set the text variable name to "myText" and open the Strings panel from the menu "Window > Other Panels > Strings". This panel lets you assign as many text as many languages you need. Simply choose your text, go to settings and choose your languages then move to the panel and give a name to this control on the ID box. Down there set the text for the different languages.
Add a new layer on Flash where to put the code below.
import fl.lang.*;
import flash.net.SharedObject;
Locale.setLoadCallback(localeListener);
import in9;
var l:Language = new Language();
addChildAt(l, 1);
Locale.loadLanguageXML( l.getLanguage() );
function localeListener(success:Boolean):void {
if (success) {
myText.text = Locale.loadString("IDS_TEST");
} else {
myText.text = "unable to load language XML file.";
}
}
addEventListener("onChangeLanguage", onChangeLanguage);
function onChangeLanguage( event:Event ):void {
Locale.loadLanguageXML( l.getLanguage() );
}
Shortly you have here the fl.lang class for working with languages and the SharedObject for working with cookies. I use a cookie indeed to keep track of a user preference concerning the language.
Object "l" is the following class "Language.as":
package
{
import flash.display.MovieClip;
import flash.net.SharedObject;
import flash.system.Capabilities;
public class Language extends MovieClip
{
private var _so:SharedObject;
public function Language()
{
_so = SharedObject.getLocal("innove.it","/");
}
public function setLanguage( s:String ):void {
_so = SharedObject.getLocal("innove.it","/");
_so.data.l = s;
_so.flush();
}
public function getLanguage():String {
return _so.data.l;
}
public function getLanguageToWholeString():String {
switch( _so.data.l ) {
case "fr":
return "FRANÇAIS";
break;
case "en":
return "ENGLISH";
break;
case "it":
return "ITALIANO";
break;
}
return "ENGLISH";
}
}
}
You can copy and paste to use it as is. Instead of "innove.it" set the name you want to give to your own cookie. That's it.
The function "localeListener" is the callback function that is called after the XML file is loaded, so whenever you then call "Locale.loadLanguageXML( l.getLanguage() );"
And what about changing language from Flex?
Suppose to have a button or an object where you set this up:
myMenu.addEventListener(MouseEvent.CLICK, onClickLanguage);
then you simply put:
private function onClickLanguage( event:MouseEvent ):void {
yourLoader.content.dispatchEvent(new Event("onChangeLanguage"));
}
This will cause an event dispatch that will be afterwards caught at runtime from the code I earlier wrote on Flash.
When the function is called I dare say that you have already changed the language on your cookie via another method.
If you have troubles with the fl.lang class, here it is how to import into your workspace:
1. Preferences -> Flex -> Editors -> ActionScritp Code -> uncheck Remove unused imports when organizing
2. First add reference to flex builder
Preferences -> General -> Workspace -> Linked Resources -> New -> Folder
Name: fl
Location: C:\Program Files (x86)\Adobe\Adobe Flash CS3\en\Configuration\ActionScript 3.0\Classes
Then add references to each project: Project Properties -> Actionscript Build Path -> Source path Tab -> new -> enter ${fl}
A useful example ( despite it is only in Italian ) for the multilanguage restricted to Flash is: http://flash.html.it/articoli/leggi/2173/applicazione-multilingua-in-flash/1/
Ciao, Matt
Dalla stessa categoria
Install SpamAssassin on Windows Server 2008 as a service
4 minuti di lettura