Skip to content

Instantly share code, notes, and snippets.

@Eiyeron
Last active December 7, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eiyeron/36000ef40fc9d343d0cd2da507b6a3eb to your computer and use it in GitHub Desktop.
Save Eiyeron/36000ef40fc9d343d0cd2da507b6a3eb to your computer and use it in GitHub Desktop.
class LanguageSelectionState extends FlxFSMState<BattleEngine> {
var localeButtons:Array<FlxButton>;
var localeFlags:Array<FlxSprite>;
var locales:Array<String>;
var background:FlxSprite;
var okButton:FlxButton;
public function new()
{
super();
background = new FlxSprite(0, 0);
var color = FlxColor.BLACK;
background.makeGraphic(640, 480, color);
background.alpha = 0.5;
localeButtons = [];
localeFlags = [];
// Global was already initiated with en-US.
locales = Global.tongue.locales;
var i = 0;
var length:Float = Global.tongue.locales.length;
var sizeUsed:Float = 50*(length+1);
var offset:Float= (FlxG.height - sizeUsed)/2;
for (locale in locales) {
var locale_text = Global.tongue.getIndexString(IndexString.Language, locale);
var button = new FlxButton(0, 50 * i+offset, locale_text);
button.scale.set(2,2);
button.updateHitbox();
button.label.scale.set(2,2);
button.label.updateHitbox();
button.x = (FlxG.width - button.width)/2.;
for(offset in button.labelOffsets)
offset.y += 4;
var flag = new FlxSprite(0,0);
flag.loadGraphic(Global.tongue.getIcon(locale));
flag.scale.set(2,2);
flag.updateHitbox();
flag.setPosition(button.x - flag.width - 2, button.y+8);
localeButtons.push(button);
localeFlags.push(flag);
i++;
}
okButton = new FlxButton(0, offset + sizeUsed + offset/2, "OK");
okButton.scale.set(2,2);
okButton.updateHitbox();
okButton.x = (FlxG.width - okButton.width)/2.;
okButton.label.scale.set(2,2);
okButton.label.updateHitbox();
for(offset in okButton.labelOffsets)
offset.y += 4;
}
override public function enter(owner:BattleEngine, fsm:FlxFSM<BattleEngine>):Void
{
owner.language_selected = false;
owner.add(background);
for(button in localeButtons) {
button.onDown.callback = function() {
var index = localeButtons.indexOf(button);
Global.language = locales[index];
Global.tongue.init(locales[index]);
for(button in localeButtons) {
var locale_text = Global.tongue.getIndexString(IndexString.Language, Global.tongue.locales[localeButtons.indexOf(button)]);
button.text = locale_text;
}
}
owner.add(button);
}
okButton.onUp.callback = function() {
owner.language_selected = true;
}
for(flag in localeFlags) {
owner.add(flag);
}
owner.add(okButton);
}
override public function exit(owner:BattleEngine):Void{
owner.textbox.dismiss();
owner.remove(background);
for(button in localeButtons)
owner.remove(button);
for(flag in localeFlags)
owner.remove(flag);
owner.remove(okButton);
var tutorial_save = new FlxSave();
if(tutorial_save.bind("tutorial")) {
tutorial_save.data.language = Global.language;
trace("Language saved");
tutorial_save.close();
}
else {
FlxG.log.error("couldn't open save");
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<data>
<file id="battle" value="battle.tsv"/>
<file id="special_attacks" value="special_attacks.tsv"/>
<file id="items" value="items.tsv"/>
<file id="enemies" value="enemies.tsv"/>
<!--American English-->
<locale id="en-US" is_default="true" sort="0">
<contributors value="Eiyeron"/>
<ui language="Language" region="Region" accept="OK" />
<label id="en-US" language="English" region="U.S."/>
<label id="fr-FR" language="Anglais" region="U.S.A."/>
</locale>
<!--French French-->
<locale id="fr-FR">
<contributors value="Eiyeron"/>
<ui language="Langage" region="Région" accept="OK" />
<label id="en-US" language="French" region="France"/>
<label id="fr-FR" language="Français" region="France"/>
</locale>
</data>
@larsiusprime
Copy link

larsiusprime commented Dec 6, 2016

Alrighty, so say you want to create a menu that looks like this:

Language Region
English U.S.A.
French (Français) France (France)

But as soon as you click the "french" button you want it to look like this:

Langage Région
Anglais (English) U.S.A. (U.S.A)
Français France

And then you want the "okay" button to show the properly localized text as well. Basically, you want each entry to be displayed in the currently selected language, and then if that locale is not the same as what's selected, in parenthesis you want to show it in its own language. This is a good way to do it so that it's impossible to get "lost" in the localization menu by clicking the wrong thing.

This is what this XML file and the attributes stuff is for -- being able to get this metadata to build such a menu without having to reload firetongue on each click.

First, let's get major descriptor words like "Language" and "Region" to build the menu itself before we do the entries:

//get the word "language" for the menu header
var language = getIndexString(IndexString.TheWordLanguage, myLocale);

//get the word "region" for the menu header
var region = getIndexString(IndexString.TheWordRegion, myLocale);

//get an arbitrary word from the index
var arbitraryWord = getIndexAttribute(myLocale, "word", "arbitrary");
<locale id="en-US" is_default="true" sort="0">	
	<contributors value="Eiyeron"/>
	<ui language="Language" region="Region" accept="OK" />
	<label id="en-US" language="English" region="U.S."/>
	<label id="fr-FR" language="Anglais" region="U.S.A."/>
</locale>

This is properly set up so that the above syntax will be able to fetch "language" and "region." However, it won't be able to return "arbitraryWord", for that we need to add some stuff:

<arbitrary value="Blah blah" word="Arbitrary"/>

Now this line will work:

var arbitraryWord = getIndexAttribute(myLocale, "word", "arbitrary");

What is the purpose of this? I add this custom attribute:

<status value="100%" word="Status"/>

So that for each locale, I can display the current completion status of the localization.

This will get the word "status" itself in the given locale:

var statusWord = getIndexAttribute(myLocale, "word", "status");

And this will get the actual value for an individual entry:

var statusValue = getIndexAttribute(myLocale, "value", "status");

As you can see, all getIndexAttribute() does is directly peek into the xml.

@Eiyeron
Copy link
Author

Eiyeron commented Dec 7, 2016

Would be there a way to get a specific Language tag from a given other language, something like

getIndexString(index:String, id:String, locale:String);

Because I can't make tjhe menu change the button labels without having to reload FireTongue, unless it's the intended behaviour.

(SOrry, I'm not so clever)

Edit: I'm trying to edit the labels directly on a call back, I'd like to reproduce that logic without having to call init.

           button.onDown.callback = function() {
                var index = localeButtons.indexOf(button);
                currentLocale = locales[index];

                // Global.language = locales[index];
                Global.tongue.init(locales[index]);
                for(butt in localeButtons) {
                    var buttonLocale = Global.tongue.locales[localeButtons.indexOf(butt)];

                    var locale_text = 
                    Global.tongue.getIndexString(IndexString.LanguageBilingual, buttonLocale)
                    ;
                    butt.text = locale_text;
                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment