Using with JQuery

There is no hard dependency on JQuery. If not used i18next will use drop in functions for ajax,...

But if JQuery is loaded before i18next it can be optionally extended:

By default i18next will extend jquery by appending i18n to $ and providing a $.fn to translate dom elements marked with the data-i18n attribute.
// with options
.i18n.init({ lng: "en-US" });
// "later"
var x = $.t("key");

JQuery deferred instead of callback

Use deferred:

i18n.init(opts).done(function() {
var x = $.t("key");

Using the selector function



Hint: You can deactivate this features by setting options on init:

  • setJqueryExt = false
  • (will deactivate $.t too!)

Replace text of one element:

<a id="btn1" href="#" data-i18n=""></a>

The button will get it's text attribute set depending on it's key.

Like in the translation function '$.t(key)' you can prepand namespace with 'myNamespace:'

You could change the dataAttributes name to something else if needed, just set 'selectorAttr' on init (default is 'data-i18n').

i18next will grab a missing value from content and post it if postMissing feature is activated. You can turn this off by setting option ''defaultValueFromContent' to false on init.

empty data-i18n attribute:

the key will be taken from the elements text or value, eg: <a id="btn1" href="#" data-i18n></a>

Using Options:

You can pass in same options as in the translation function. For more information about the options go to the features page.

Alternatively, by setting init option or translation option `{useDataAttrOptions: true}` the Options for translation will be read and cached in the element's jQuery data under the "i18n-options" key. The next time `.i18n()` is called on the element, the previous options will be used. This makes it possible to retranslate the element without supplying the original arguments.

Replace on elements children:

<ul class="nav">
<li><a href="#" data-i18n="nav.home"></a></li>
<li><a href="#" data-i18n="nav.page1"></a></li>
<li><a href="#" data-i18n="nav.page2"></a></li>

Every li element will get it's text attribute set depending on it's key.

Replace on inner element:

<div class="outer" data-i18n="ns:key" data-i18n-target=".inner">
<input class="inner" type="text"></input>

As the target attribute is set the input tag with class 'inner' will get his value set.

setting html, prepend, append content:

Replace other attributes or innerHtml:

<a id="btn1" href="#" data-i18n="[title]key.for.title;key.for.text"></a>

Just provide the attribute to set in '[]' and seperate multiple attributes via ';'.

You can even set the innerHtml by prepending '[html]' or prepend by '[prepend]' or append by '[append]'.

setting data:

set data:

<a id="btn1" href="#" data-i18n="[data-someDataAttribute]key.for.text"></a>

The element will have 'data-someDataAttribute' attribute and data could be read by $("#btn1").data('someDataAttribute');