-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dan N
committed
Oct 17, 2019
1 parent
9c56532
commit 810671b
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @file jquery.translate.js | ||
* @brief jQuery plugin to translate text in the client side. | ||
* @author Manuel Fernandes | ||
* @site | ||
* @version 0.9 | ||
* @license MIT license <http://www.opensource.org/licenses/MIT> | ||
* | ||
* translate.js is a jQuery plugin to translate text in the client side. | ||
* | ||
*/ | ||
|
||
(function($){ | ||
$.fn.translate = function(options) { | ||
|
||
var that = this; //a reference to ourselves | ||
|
||
var settings = { | ||
css: "trn", | ||
lang: "ro", | ||
t: { | ||
"translate": { | ||
en: "translate" | ||
} | ||
} | ||
}; | ||
settings = $.extend(settings, options || {}); | ||
if (settings.css.lastIndexOf(".", 0) !== 0) //doesn't start with '.' | ||
settings.css = "." + settings.css; | ||
|
||
var t = settings.t; | ||
|
||
//public methods | ||
this.lang = function(l) { | ||
if (l) { | ||
settings.lang = l; | ||
this.translate(settings); //translate everything | ||
} | ||
|
||
return settings.lang; | ||
}; | ||
|
||
|
||
this.get = function(index) { | ||
var res = index; | ||
|
||
try { | ||
res = t[index][settings.lang]; | ||
} | ||
catch (err) { | ||
//not found, return index | ||
return index; | ||
} | ||
|
||
if (res) | ||
return res; | ||
else | ||
return index; | ||
}; | ||
|
||
this.g = this.get; | ||
|
||
|
||
|
||
//main | ||
this.find(settings.css).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)); | ||
}); | ||
|
||
|
||
return this; | ||
|
||
|
||
|
||
}; | ||
})(jQuery); |