EDIT:
@baris error is gone / fixed. Thank you!
languages/en-GB/map.json
{
"my_location": "My Location"
}
How can I access this value from my client-side JavaScript code?
require(['translator'], function (translator) {
translator.translate('[[global:my_location]]', function (translated) {
translated === 'My Location';
});
});
Thank you.. Pita
Successful .. Now I recalled, it was there in the documentation.
It is little cumbersome though.. if I have tens of translations, I need to write quite a number of lines of code.
What's the context here? You shouldn't need to do all of the translating yourself.
Sorry I didn't get you
Why are you doing a bunch of translations yourself? You should be able to depend on existing methods instead of doing them separately.
What I meant to say was.. consider that I want 10 translated strings, I am supposed to write this piece of code 10 times...
translator.translate('[[global:my_first]]', function (translated) {
...
});
translator.translate('[[global:my_fsecond]]', function (translated) {
...
});
And so on...
Right, and I'm asking why you are translating these strings yourself in the first place.
Simply because of internationalization. Right now, I am targeting en-GB only. Later on, may be, we need to support 'n' different number of languages. So my map.js (client side) will remain untouched.
That's not what I'm asking. I'm asking why you want to do the parsing of the translations manually instead of using the automatic translation that NodeBB does.
I am not aware of this automatic translation. Could you pl. write an example about how this can be achieved, in my case, global:my_location.
Please provide the code where you use these translations. It depends on what you're doing.
require(['translator'], function (translator) {
translator.translate('[[map:my_location]]', function (translated) {
my_location = translated;
});
});
....
marker = L.marker(initialLocation, {
draggable: true,
title: my_location,
alt: my_location,
riseOnHover: true
}).addTo(map)
.bindPopup(initialLocation.toString()).openPopup();
Ok so this is one of the rare cases where manually translating is your only option.
If you want to get a bunch of translations, I'd suggest using a filename besides global.json
, maybe use map.json
instead. Then, you can do this:
require(['translator'], function (translator) {
translator.getTranslations(translator.getLanguage(), 'map', function (translations) {
translations.my_location === 'My Location';
});
});
Also, please use code blocks from now on. It works like this:
```
// code goes here
```
Great! I understand
btw where to use the code blocks?
Use code block when you make a post with code in it
oh.. Right