DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 2.3 Checking the System Language

2.3.1 Problem

You want to know what language is used on the computer playing the movie.

2.3.2 Solution

Use the System.capabilities.language property.

2.3.3 Discussion

You can use the System.capabilities.language property to determine the language of the computer that is playing the movie. The property returns a two-letter ISO-639-1 language code (i.e., "fr" for French). Where applicable, a two-letter country code is appended, separated from the language code with a hyphen (i.e., "en-US" for U.S. English and "en-UK" for U.K. English).

For a summary of language codes, see the following resources:

http://lcweb.loc.gov/standards/iso639-2/englangn.html
http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html

Here is an example of how to use the language property:

// Example output: en-US
trace(System.capabilities.language);

You can use this property to dynamically load content in the appropriate language:

// Create an associative array with language codes 
// for the keys and greetings for the values.
greetings = new Array(  );
greetings["en"] = "Hello";
greetings["es"] = "Hola";
greetings["fr"] = "Bonjour";

// Extract the first two characters from the language code.
lang = System.capabilities.language.substr(0, 2);

// Use a default language if the language is not in the list.
if (greetings[lang] == undefined) {
  lang = "en";
}

// Display the greeting in the appropriate language.
trace(greetings[lang]);

When you want to offer multiple language capabilities in your movies, you can choose from several different approaches. One approach, as shown in the preceding code, is to create associative arrays for all the text that appears in the movie. Another is to create static content in multiple movies (one for each language) and load those movies based on the language code. With this technique, each .swf filename should include the language code, such as myMovie_en.swf, myMovie_es.swf, myMovie_fr.swf, etc.

// Get the language from the capabilities object.
lang = System.capabilities.language.substr(0, 2);

// Create an array of the languages you are supporting (i.e., the languages for which
// you have created movies).
supportedLanguages = ["en", "es", "fr"];

// Set a default language in case you don't support the user's language.
useLang = "en";

// Loop through the supported languages to find a match to the user's language. If
// you find one, set useLang to that value and then exit the for statement.
for (var i = 0; i < supportedLanguages.length; i++) {
  if (supportedLanguages[i] == lang) {
    useLang = lang;
    break;
  }
}

// Load the corresponding 

movie.
_root.loadMovie("myMovie_" + useLang + ".swf");
    [ Team LiB ] Previous Section Next Section