Andy_Parr 0 Light Poster

Hi all,

I'm trying to make a multi-language website that allows the user to change language by clicking on a button (a flag representing the country/language). I have been able to achieve this using locale and a combo box so far but I'm struggling to implement it using buttons.

This is my code so far:

ActionScript Code:
import fl.data.DataProvider;
import flash.events.Event;
import fl.events.DataChangeEvent;

//------------------
// SETUP LOCALE OBJECT:

var languages:Object = new Object();    // Stores flags for loaded languages
var localeDefault:String = "en";     // Default language
var locale:String = "en";    // Current language selected in combobox

// Event handler for Locale object
function localeLoadedHandler(success:Boolean):void 
{
if( success )
{
// Mark language as loaded and show localized string
languages[locale] = true;
field_001.text = Locale.loadStringEx("IDS_001", locale);
}
} 
// Load the default language...
Locale.setDefaultLang(localeDefault);
Locale.setLoadCallback(localeLoadedHandler);
Locale.loadLanguageXML(Locale.getDefaultLang());

//------------------
// SETUP COMBOBOX:

// Event handler for combobox changes
function langListener(event:Event):void 
{
// Set locale from combobox
locale = event.target.selectedItem.data;

// Load language XML 
if(!languages[locale]){
Locale.loadLanguageXML(locale);
}
// Or show text if it's already loaded
else{
field_001.text = Locale.loadStringEx("IDS_001", locale);
}
}

// Get list of Locales from Strings panel
var locales:Array = Locale.languageCodeArray.sort();    
var len:uint = locales.length;  
var dp:Array = new Array(); 

// Format combobox labels
for(var n:uint=0; n<len; n++)
{
var lang:String = locales[n];
var descr:Object = new Object();
switch( lang )
{
case "en":
descr.label = "en (English)";
descr.data = "en";
break;

case "es":
descr.label = "es (Spanish)";
descr.data = "es";
break;
}
dp.push(descr);
}
// Setup combobox
locales_cb.addEventListener(Event.CHANGE, langListener);
locales_cb.dataProvider = new DataProvider(dp);

Is there a way I could do something similar so when the user clicks on a button on the page it changes the locale variable and therefore updates the language.

Thanks in advance, Andy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.