mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
[enh] Add i18n \o/ 👏
This commit is contained in:
parent
e27749799a
commit
903885bb09
5 changed files with 123 additions and 0 deletions
|
@ -48,6 +48,7 @@
|
|||
<script type="text/javascript" src="js/vendor/sammy.storage.js"></script>
|
||||
<script type="text/javascript" src="js/vendor/bootstrap.min.js"></script>
|
||||
<script type="text/javascript" src="phonegap.js"></script>
|
||||
<script type="text/javascript" src="js/y18n.js"></script>
|
||||
<script type="text/javascript" src="js/app.js"></script>
|
||||
<script type="text/javascript">
|
||||
app.initialize = app.initialize || function(){}
|
||||
|
|
27
js/app.js
27
js/app.js
|
@ -29,6 +29,11 @@ app = Sammy('#main', function (sam) {
|
|||
return Math.round(bps / Math.pow(1024, i), 2) + ' ' + sizes[[i]] + '/s';
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('t', function(y18n_key) {
|
||||
var result = y18n.t(y18n_key, Array.prototype.slice.call(arguments, 1));
|
||||
return new Handlebars.SafeString(result);
|
||||
});
|
||||
|
||||
|
||||
// Look for supported type of storage to use
|
||||
var storageType;
|
||||
|
@ -1003,7 +1008,29 @@ app = Sammy('#main', function (sam) {
|
|||
* Run the app
|
||||
*
|
||||
*/
|
||||
|
||||
$(document).ready(function () {
|
||||
|
||||
/**
|
||||
* Translations
|
||||
*/
|
||||
|
||||
// Default language
|
||||
$.getJSON('locales/en.json', function(data){
|
||||
y18n.translations['en'] = data;
|
||||
});
|
||||
|
||||
// User language
|
||||
if (window.navigator && window.navigator.language) {
|
||||
y18n.locale = window.navigator.language;
|
||||
$.getJSON('locales/'+ y18n.locale +'.json', function(data){
|
||||
y18n.translations[y18n.locale] = data;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Application
|
||||
*/
|
||||
app.run('#/');
|
||||
|
||||
// Fixes for sliding effect
|
||||
|
|
93
js/y18n.js
Normal file
93
js/y18n.js
Normal file
|
@ -0,0 +1,93 @@
|
|||
;(function(y18n){
|
||||
"use strict";
|
||||
|
||||
// Default options
|
||||
var defaultOptions = {
|
||||
defaultLocale: "en",
|
||||
locale: "en",
|
||||
placeholder: /(?:\{\{|%\{)(.*?)(?:\}\}?)/gm,
|
||||
translations: {},
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialization
|
||||
*/
|
||||
y18n.init = function() {
|
||||
// Merge options with defaults.
|
||||
for (var key in defaultOptions) {
|
||||
y18n[key] = (typeof y18n[key] !== 'undefined') ? y18n[key] : defaultOptions[key];
|
||||
}
|
||||
}
|
||||
y18n.init();
|
||||
|
||||
|
||||
/**
|
||||
* Translation
|
||||
*/
|
||||
y18n.translate = function(key, options) {
|
||||
options = options || {'locale' : y18n.locale};
|
||||
options.locale = options.locale || y18n.locale;
|
||||
|
||||
// Get translation
|
||||
var translation = this.lookup(key, options);
|
||||
|
||||
// Translation fallback
|
||||
if ((typeof translation === 'undefined' || translation === undefined) && options.locale !== y18n.defaultLocale) {
|
||||
options.locale = y18n.defaultLocale;
|
||||
return this.translate(key, options);
|
||||
}
|
||||
|
||||
// Variables remplacement
|
||||
return (translation) ? translation.printf(options) : undefined;
|
||||
}
|
||||
|
||||
y18n.lookup = function(key, options) {
|
||||
// Default locale
|
||||
if (typeof options.locale === 'undefined') {
|
||||
options.locale = y18n.locale;
|
||||
}
|
||||
|
||||
// Get translation string
|
||||
if (typeof y18n.translations[options.locale] !== 'undefined') {
|
||||
if (typeof y18n.translations[options.locale][key] !== 'undefined') {
|
||||
return y18n.translations[options.locale][key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save some typing
|
||||
y18n.t = y18n.translate;
|
||||
|
||||
})(typeof(exports) === 'undefined' ? (this.y18n || (this.y18n = {})) : exports);
|
||||
|
||||
// http://monocleglobe.wordpress.com/2010/01/12/everybody-needs-a-little-printf-in-their-javascript/
|
||||
String.prototype.printf = function (obj) {
|
||||
var useArguments = false;
|
||||
var _arguments = arguments;
|
||||
var i = -1;
|
||||
if (typeof _arguments[0] == "string") {
|
||||
useArguments = true;
|
||||
}
|
||||
if (obj instanceof Array || useArguments) {
|
||||
return this.replace(/\%s/g,
|
||||
function (a, b) {
|
||||
i++;
|
||||
if (useArguments) {
|
||||
if (typeof _arguments[i] == 'string') {
|
||||
return _arguments[i];
|
||||
}
|
||||
else {
|
||||
throw new Error("Arguments element is an invalid type");
|
||||
}
|
||||
}
|
||||
return obj[i];
|
||||
});
|
||||
}
|
||||
else {
|
||||
return this.replace(/{([^{}]*)}/g,
|
||||
function (a, b) {
|
||||
var r = obj[b];
|
||||
return typeof r === 'string' || typeof r === 'number' ? r : a;
|
||||
});
|
||||
}
|
||||
};
|
1
locales/en.json
Normal file
1
locales/en.json
Normal file
|
@ -0,0 +1 @@
|
|||
{}
|
1
locales/fr.json
Normal file
1
locales/fr.json
Normal file
|
@ -0,0 +1 @@
|
|||
{}
|
Loading…
Add table
Reference in a new issue