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

Tooltips and tokens #123

Closed mcallegari closed 4 years ago

mcallegari commented 5 years ago

Hello, I'm trying to use this plugin, but there's one thing not working for me.

<span class="a b c" lang="en" data-lang-token="_myToken" title="Quite a long text to display some information about this element" ></span>

The plugin tries to lookup the entire string (inefficient) rather than the token (efficient). Is there a workaround/fix for this?

Irrelon commented 5 years ago

Can you describe how you came to the conclusion that it is looking up the entire string? I haven’t used the plugin for years because no one uses jQuery in production anymore but I don’t remember it having any issues like this...

mcallegari commented 5 years ago

I see an error message Translation for "Quite a long text to display some information about this element" not found in language pack: xy By the way I fixed the code. Might submit a pull request but atm I'm quite busy.

mcallegari commented 5 years ago

In _storeAttribs, I translate directly the attribute if both lang and data-lang-token are defined.

262,263c262,271
<                 // Add the attribute and value to the store
<                 attrObj[attr] = elem.attr(attr);
---
>                       var eToken = elem.data('langToken');
>                       if (eToken) {
>                               var t = this.translate(eToken, this.currentLang);
>                               //console.log("data token found: " + eToken + ", translation: " + t);
>                               elem.attr(attr, t);
>                       }
>                       else {
>                               // Add the attribute and value to the store
>                               attrObj[attr] = elem.attr(attr);
>                       }
Irrelon commented 4 years ago

Hi ya, this might be fixed by the update pushed today to version 4.0.0.

Irrelon commented 4 years ago

The trick is to set the title to the token you want to use rather than the initial text. You also need to make sure you have a language pack for your default language and make sure you've got a translation from the token to the text you want the title attribute to display (but only version 4.0.0).

Irrelon commented 4 years ago

Closing for now, please re-open if version 4.0.0 doesn't solve the problem for you!