Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ Step 1: include JQuery and translate.js in your page
<script src="jquery.js"/>
<script src="jquery.translate.js"/>

Step 2: every text you want translated include the trn class
Step 2: there are multiple ways to translate your text, you can add the trn class, use the trn tag or translate attribute content with trn-attr class and a data-trn-attr attribute containing a space separated list of attributes that must be translated

<span class="trn">text to translate</span>
<span><trn>text to translate</trn></span>
<a href="/test" title="title to translate" class="trn-attr" data-trn-attr="title">Go!</a>

Instead using your text as key you can use the data-trn-key attribute to specify the key or the data-trn-key-attribute-name attribute to specify the key to use for attribute-name translation.

Step 3: create your dictionary

Expand Down
47 changes: 38 additions & 9 deletions jquery.translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@

var settings = {
css: "trn",
lang: "en"/*,
t: {
"translate": {
pt: "tradução",
br: "tradução"
}
}*/
tag: "trn",
css_attr: "trn-attr",
lang: "en",
debug: false,
};
settings = $.extend(settings, options || {});
if (settings.css.lastIndexOf(".", 0) !== 0) //doesn't start with '.'
settings.css = "." + settings.css;

if (settings.css_attr.lastIndexOf(".", 0) !== 0) //doesn't start with '.'
settings.css_attr = "." + settings.css_attr;

var t = settings.t;

//public methods
Expand All @@ -50,6 +49,9 @@
}
catch (err) {
//not found, return index
if (settings.debug) {
console.log("No '" + settings.lang + "' translation for '" + index + "'");
}
return index;
}

Expand All @@ -64,6 +66,17 @@


//main
this.find(settings.tag).each(function(i) {
var $this = $(this);

var trn_key = $this.attr("data-trn-key");
if (!trn_key) {
trn_key = $this.html();
$this.attr("data-trn-key", trn_key); //store key for next time
}

$this.html(that.get(trn_key));
});
this.find(settings.css).each(function(i) {
var $this = $(this);

Expand All @@ -75,7 +88,23 @@

$this.html(that.get(trn_key));
});

this.find(settings.css_attr).each(function(i) {
var $this = $(this);

var trn_attr_names = $this.attr("data-trn-attr");
trn_attr_names = trn_attr_names.split(" ");
// for each attribute
for (i = 0; i < trn_attr_names.length; i++) {
trn_attr_name = trn_attr_names[i];
var trn_key = $this.attr("data-trn-key-" + trn_attr_name);
if (!trn_key) {
trn_key = $this.attr(trn_attr_name);
$this.attr("data-trn-key-" + trn_attr_name, trn_key); //store key for next time
}
$this.attr(trn_attr_name, that.get(trn_key));
}
});


return this;

Expand Down