Irrelon / jquery-lang-js

i18n Allow instant language switching on HTML pages without reloading the page.
https://www.irrelon.com
362 stars 132 forks source link

How to translate dynamic alert content #125

Closed dianeryanto closed 4 years ago

dianeryanto commented 4 years ago

Hi Sir, thanks for your great work it's really help.

But i found some problem, i don't know how to translate dynamic alert content. Already read your documentation, you suggest using window.lang.translate for alert, but it for static content.

Example for dynamic alert content alert("Are you sure to delete "+name+" product");

Any idea for solve this problem?

Thanks before Sir.

Irrelon commented 4 years ago

Hi there!

I've just made a large update to do what you need. Please see the readme file on "template strings": https://github.com/Irrelon/jquery-lang-js#dynamic-data--template-strings

This new version is the first large update for a long time and I don't think I broke anything but let me know if you have any issues.

Irrelon commented 4 years ago

P.S. you will need to update to version 4.0.0 in order to use this new feature.

Irrelon commented 4 years ago

Closing as this has now been resolved ;)

dianeryanto commented 4 years ago

Awww, i dont think you'll response me but you just made new update for my needs.

Really big thanks Sir! šŸ‘

dianeryanto commented 4 years ago

Hi Sir, your update is working perfectly. But i think i have new issue.

Based on your example, we must define like this for dynamic alert content.

"token": { "myTranslationToken":"Hello ${data.firstName} ${data.lastName}" }

alert(window.lang.translate("myTranslationToken", "en", {"firstName: "Amelia", "lastName": "Earhart"}));

Result : Hello Amelia Earhart

The problem is how to create dynamic default language alert content too? Can i create myTranslationToken for dynamic default language too?

My Expected Result like this

Default Language: Halo Amelia Earhart and after translate become Hello Amelia Earhart

In html tag we define like this

<div lang="id">Default Language</div> "token": { "Default language":"Translate from Default Language" } When we change language, the result become <div lang="en">Translate from Default Language</div>

Any idea sir @Irrelon ? Thanks before Sir, i hope im not bother you

Irrelon commented 4 years ago

Hey ya! No problem to ask questions! If I have time I will always try to help.

You can create a language json file for your default language and load it the same way you load the other languages.

If your default language is ā€œidā€ and your second language is ā€œenā€ then you would create two json files, one called ā€œid.jsonā€ and one called ā€œen.jsonā€ and then both will have the token ā€œmyTranslationTokenā€ with their respective value ā€œhalo ...ā€ and ā€œhello...ā€

Does that make sense? Iā€™m replying from my phone at the moment so I canā€™t insert code examples but the readme.md was updated and includes an example :)

Please let me know if that helps you :)

dianeryanto commented 4 years ago

Already try your suggest to create 2 different json id.json and en.json and both of them have token myTranslationToken with different language.

Load the json

var lang = new Lang(); lang.dynamic('en', '/plugin/jquery-lang-js-master/js/langpack/en.json'); lang.dynamic('id', '/plugin/jquery-lang-js-master/js/langpack/id.json'); lang.init({ defaultLang: 'id'})`

But when i try to change language to en -> id or id -> en,
The output still in one language, not dynamically change.

my alert like this alert(window.lang.translate("myTranslationToken", "id", [value1, value2]));

Am i wrong or miss something?

dianeryanto commented 4 years ago

Sir, i think i got trick for solve my problem but i think not best solution.

i create actual_lang variable who dynamically change based on what languages we choose and put it on on second parameter.

Like this and working perfectly. alert(window.lang.translate("myTranslationToken", actual_lang, [value1, value2]));

So i think i can close my problem. Thanks for your help sir.

Irrelon commented 4 years ago

Hey! I'm at my computer now.

I fully understand what your problem is now! :)

So for the translate() method you needed to be able to "fill in" the current selected language.

This is very simple. Just pass undefined as the second parameter :)

e.g.

alert(window.lang.translate("myTranslationToken", undefined, [value1, value2]));

When you don't specify the language to use, the translate() method will default to the current language.

dianeryanto commented 4 years ago

Yes, already try using undefined or get the actual language from cookies and both of them working perfectly. But i prefer using undefined based on your suggest as project owner šŸ˜„

Thanks for your help sir !