Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm using the following script for my projects. It allows you to change the language "at runtime", without reloading the page. The script is "autorun", just add it at the end of the html page. It could have some bugs ;)</p> <pre><code> // AutoStar! // Grab the parameters from my url, and initialize myself! FUGOOOOO (function __lang_init_wrapper() { var scriptSrc = $('script[src*=Lang]').attr('src'); var myParams = parseParams(scriptSrc); new Lang(myParams.language.toUpperCase(), true); })(); /** * Thanks to: http://wowmotty.blogspot.com/2010/04/get-parameters-from-your-script-tag.html * @param n * @param s */ function gup(n,s){ n = n.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]"); var p = (new RegExp("[\\?&amp;]"+n+"=([^&amp;#]*)")).exec(s); return (p===null) ? "" : p[1]; } /** * * @param language The language to use * @param replaceText If true, replace all the occurency marked with placemark {lang=&lt;key&gt;} */ function Lang(language, replaceText) { var Languages = { ENG: { ok: 'ok' ,yes: 'yes' ,no: 'no' ,unknown_user: 'Unknown user' ,too_soon: "It's not time, yet..!" } ,ITA: { yes: 'si' ,unknown_user: 'Utente sconosciuto' ,too_soon: "Pazienta ancora un po'..!" } } // GENERAL SETTINGS var LANG_CURRENT = language; var LANG_DEFAULT = 'ENG'; /** * All the html elements with this attributes are translated on the fly */ var LANG_ATTRIBUTE_NAME = "uilang" /** * key è la chiave da usare nell'oggetto LANG * @param key */ this.get = function(key) { return Languages[LANG_CURRENT][key] || Languages[LANG_DEFAULT][key]; } /** * Cerco tutti gli elementi che hanno una certa classe */ this.searchAndReplace = function() { var me = this; var divs = $('*[' + LANG_ATTRIBUTE_NAME + ']'); $.each(divs,function(indx,item) { item = $(item); item.text(me.get(item.attr(LANG_ATTRIBUTE_NAME))); }); } this.setLanguage = function(language, replaceText) { LANG_CURRENT = language; if(replaceText){ this.searchAndReplace(); } } if(replaceText){ this.searchAndReplace(); } // Returns a localized instance of language Lang = { get: this.get ,searchAndReplace: this.searchAndReplace ,setLanguage: this.setLanguage }; } </code></pre> <p>TO use it, just "mark" an html element</p> <pre><code>&lt;h1 uilang="unknown_user"&gt;&lt;/h1&gt; </code></pre> <p>or call</p> <pre><code>Lang.get('unknown_user') </code></pre> <p>to get the localized string</p> <p>To initialize, call the "constructor"</p> <pre><code>new Lang("ITA", true); </code></pre> <p>To use it specifyng a language, </p> <pre><code>&lt;script type="text/javascript" src="js/Lang.js?language=ita"&gt;&lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload