I'm trying to translate a select drop-down and I don't like the way I'm doing it, because it's cumbersome and it bypasses the whole angular-translate framework.
The locale data looks like {"lang": {"label": "text", "select": {"k1": "var1", "k2": "var2"}}} and if I plonk the "select" member in the controller scope, I can write something like "k as v for (k,v) in scopedvar" in the ng-options of the select.
Basically I'd like translate to do the language resolution and then get out of the way and return the map of localizations for my options. If it made sense, something like: "k as v for (k, v) in 'select' | translate", but of course it doesn't.
Has anyone faced (and solved) this issue before?
TIA, Edoardo
ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat.
The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.
NGX-Translate is an internationalization library for Angular. It lets you translate for your content in different languages and switch between them easily.
AngularJS ng-options Directive 1 Definition and Usage. The ng-options directive fills a <select> element with <options>. The ng-options directive uses an... 2 Syntax. Supported by the <select> element. 3 Parameter Values. An expression that selects the specified parts of an array to fill the select element. More ...
The translation process for Angular applications consists of 5 steps: Mark all text you want to translate in your templates. Use the ng xi18n command line tool to extract the translations and create an XLIFF translation file Translate the messages in the file (e.g. by using BabelEdit) Edit the applications' configuration to recognize the new locale
Click on Angular and XLIFF to start a new translation project: Next drop your src/locale/message.en.xml onto the language configuration dialog. Select en-US as language. Click Add and New to add your first target language. Choose de-DE and change the file name to messages.de.xlf. In the last step select en-US as primary language.
title and text are identifiers ngx-translate uses to find the translation strings. Now edit the app.component.html and replace the static texts with references to the messages in the en.json file. You have 3 choices when it comes to adding translations: translation directive — id as attribute value <element [translate]="'id'"></element>
I didn't completely get what you're trying to achieve, but I'll put some code that works fine for reloading the options translations with the | translate filter.
Assuming you have this json as key/values for your translations:
var english = {"lang": {
"label": "text",
"select": {
"k1": "var1",
"k2": "var2"
}}
};
And your controller creates a list of options like this:
$scope.optionsList = [
{val: 'var1', translationKey: 'lang.select.k1'},
{val: 'var2', translationKey: 'lang.select.k2'}
];
You should be good to go inserting the translate filter after the option label in the ng-options expression:
<select ng-model="selectedOpt"
ng-options="opt.val as opt.translationKey | translate for opt in optionsList">
</select>
Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With