Skip to content

Commit

Permalink
Fix translations in items dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Shallowmallow committed Sep 8, 2024
1 parent c3ebf05 commit de3133d
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion haxe/ui/backend/hxwidgets/behaviours/ChoiceDataSource.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package haxe.ui.backend.hxwidgets.behaviours;

import haxe.ui.locale.LocaleEvent;
import haxe.ui.locale.LocaleManager;
import haxe.ui.events.UIEvent;
import haxe.ui.behaviours.DataBehaviour;
import haxe.ui.components.DropDown;
Expand All @@ -10,6 +12,13 @@ import hx.widgets.Choice;

@:access(haxe.ui.core.Component)
class ChoiceDataSource extends DataBehaviour {

public function new(component:haxe.ui.core.Component) {
super(component);
LocaleManager.instance.registerEvent(LocaleEvent.LOCALE_CHANGED, function (e) {
changeLang();
});
}
public override function get():Variant {
if (_value == null || _value.isNull) {
_value = new ArrayDataSource<Dynamic>();
Expand All @@ -28,6 +37,36 @@ class ChoiceDataSource extends DataBehaviour {
}
}

public function changeLang() {
if (_component.window == null) {
return;
}

var choice:Choice = cast(_component.window, Choice);
var oldSelection = choice.selection;
choice.clear();

if (_value.isNull) {
return;
}

var ds:DataSource<Dynamic> = _value;
for (n in 0...ds.size) {
var item = ds.get(n);
if (item.text != null) {
choice.append(LocaleManager.instance.stringToTranslation(item.text));
} else {
choice.append(Std.string(item));
}
}

var dropDown:DropDown = cast(_component, DropDown);
choice.selection = oldSelection;
if (dropDown.text != null) {
choice.selectedString = dropDown.text;
}
}

public override function validateData() {
if (_component.window == null) {
return;
Expand All @@ -44,7 +83,7 @@ class ChoiceDataSource extends DataBehaviour {
for (n in 0...ds.size) {
var item = ds.get(n);
if (item.text != null) {
choice.append(item.text);
choice.append(LocaleManager.instance.stringToTranslation(item.text));
} else {
choice.append(Std.string(item));
}
Expand Down

0 comments on commit de3133d

Please sign in to comment.