Permalink
Browse files

use i18next for localisation

Until now, we used R.js for localisation. That library was abandoned a long time ago by its creator, and wasn't very sophisticated - it lacked things like plural interpolation.
This commit switches to [i18next](http://i18next.com/), and changes the way the translated strings are included - now, every language we have is included in the compiled package, and the preferred one is used at init. This could open the way to switching languages while the exam is running.
The translated strings are stored in .json files in the locales directory. It's now possible to export languages directly from POEditor without having to put any boilerplate around the file.
  • Loading branch information...
1 parent 4c0e4eb commit ad88a15bdc8c0ef47164134073d870698cd4f5d1 @christianp christianp committed Oct 11, 2016
View
@@ -29,6 +29,8 @@
import xml.etree.ElementTree as etree
from itertools import count
import subprocess
+import json
+
namespaces = {
'': 'http://www.imsglobal.org/xsd/imscp_v1p1',
@@ -206,10 +208,23 @@ def makeExam(options):
files = collectFiles(options)
files[os.path.join('.','settings.js')] = io.StringIO(options.xmls)
- localePath = os.path.join(options.path,'locales',options.locale+'.js')
- if not os.path.exists(localePath):
- raise Exception("Can't find locale "+options.locale)
- files[os.path.join('.','locale.js')] = io.StringIO(open(localePath,encoding='utf-8').read())
+ localePath = os.path.join(options.path,'locales')
+ locales = {}
+ for fname in os.listdir(localePath):
+ name,ext = os.path.splitext(fname)
+ if ext.lower()=='.json':
+ with open(os.path.join(localePath,fname),encoding='utf-8') as f:
+ locales[name] = {'translation': json.loads(f.read())}
+
+ locale_js = """
+ Numbas.queueScript('localisation-resources',['i18next'],function() {{
+ Numbas.locale = {{
+ preferred_locale: {},
+ resources: {}
+ }}
+ }});
+ """.format(json.dumps(options.locale),json.dumps(locales))
+ files[os.path.join('.','locale.js')] = io.StringIO(locale_js)
if options.scorm:
IMSprefix = '{http://www.imsglobal.org/xsd/imscp_v1p1}'
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
Oops, something went wrong.
View
@@ -1,248 +0,0 @@
-Numbas.queueScript('R',[],function(module) {
-/*jslint white: false, browser: true, devel: true, onevar: true, undef: true,
- nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true,
- newcap: true, immed: true, maxlen: 100, indent: 4 */
-/*globals module: false*/
-/*
- * R.js - Internationalisation Library
- *
- * R.js is a simple Javascript Internationalisation library
- *
- * This code is licensed under the MIT
- * For more details, see http://www.opensource.org/licenses/mit-license.php
- * For more information, see http://github.com/keithcirkel/R.js
- *
- * @author Keith Cirkel ('keithamus') <rdotjs@keithcirkel.co.uk>
- * @license http://www.opensource.org/licenses/mit-license.php
- * @copyright Copyright © 2011, Keith Cirkel
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
-(function (exports) {
- var oldR = exports.R
- , inited = false
- , eR
- , undef
- , R = {
-
- init: function (lang) {
- lang = lang || typeof navigator !== undef ? navigator.language : 'en-GB';
- //Initialise some variables.
- eR.locales = {};
- eR.navLang = lang;
- inited = true;
- },
-
- slice: function (a) {
- return Array.prototype.slice.call(a);
- },
-
- AinOf: function (a, v) {
- for (var i = 0, c = a.length; i < c; ++i) if (a[i] === v) return i;
- return -1;
- },
-
- realTypeOf: function (v) {
- return Object.prototype.toString.call(v).match(/\w+/g)[1].toLowerCase();
- },
-
- render: function (id, args) {
- if (eR.locales && eR.locales.hasOwnProperty(eR.lang)) {
- if (eR.locales[eR.lang].hasOwnProperty(id)) {
- id = eR.locales[eR.lang][id];
- }
- }
-
- // If we've no arguments, let's return
- if (!args || args.length === 0) {
- return id;
- }
-
- // Ok, we do have args, so lets parse them.
- args = R.parseVariables(args);
-
- return R.format(id, args);
- },
-
- parseVariables: function (args, ret) {
- var i
- , c
- , type = R.realTypeOf(args);
-
- // This is our structure for formatting, numbers go in i, string in s,
- // and the named arguments (i.e %(age)) go in named.
- if (!ret) {
- ret = { i: [], s: [], named: {} };
- }
-
- //Check args to see what type it is, and add to ret appropriately.
- switch (type) {
- case 'number': ret.i.push(args); break;
- case 'string': ret.s.push(args); break;
- case 'date': ret.i.push(args.toString()); break;
- case 'object':
- for (i in args) {
- if (args.hasOwnProperty(i)) {
- if (i === 'i' || i === 's') {
- R.parseVariables(args[i], ret);
- } else {
- ret.named[i] = args[i];
- }
- }
- }
- break;
- case 'array':
- // Loop through the array, doing what we just did
- for (i = 0, c = args.length; i < c; ++i) {
- R.parseVariables(args[i], ret);
- }
- break;
- }
-
- return ret;
- },
-
- format: function (s, a) {
- var i
- , replace
- , tcount
- , substrstart
- , type
- , l
- , types = {i: '%i', s: '%s'}
- , tmp = ''
- , t;
-
- //First we'll add all integers to the pot, then the strings
- for (type in types) {
- if (types.hasOwnProperty(type)) {
- tcount = (s.match(new RegExp(types[type], 'g')) || []).length;
- for (i = 0; i < tcount; ++i) {
- replace = a[type].hasOwnProperty(i) ? a[type][i] : replace;
- if (replace !== undef && replace !== false) {
- if ((substrstart = s.indexOf(types[type])) >= 0) {
- s = s.substr(0, substrstart) + replace + s.substr(substrstart + 2);
- }
- }
- }
- }
- replace = false;
- }
-
- //Now to loop through the named arguments!
- for (i in a.named) {
- if (a.named.hasOwnProperty(i)) {
- t = new RegExp("%\\(" + i + "\\)", 'g');
- s = s.replace(t, a.named[i]);
- }
- }
-
- return s;
- }
- };
-
- /*
- * R
- * Take an `id` from the registered locales and return the string including any variables.
- * @param {String} id The ID of the
- * @param {Mixed} variables
- */
- eR = function (id, variables) {
- // If we haven't initialised our variables etc, then do so.
- if (!inited) R.init();
-
- if (arguments.length > 1) {
-
- // In the case we've been given comma separated arguments, we should
- // load them into an array and send them to render
- var args = R.slice(arguments);
- args.shift();
-
- return R.render(id, args);
-
- // Otherwise just send the `variables` var.
- } else {
- return R.render(id, [variables]);
- }
- };
-
- /*
- * R.registerLocale
- * Apply `object` of strings to `locale` for use with the main function, R
- * @param {String} locale The locale of the language object, e.g en-US
- * @param {Object} A flat object of strings
- */
- eR.registerLocale = function (locale, object) {
- //If we haven't initialised our variables etc, then do so.
- if (!inited) R.init();
-
- //Throw the object we've been given into locales.
- eR.locales[locale] = object;
- eR.setLocale(eR.navlang);
- return eR;
- };
-
- /*
- * R.localeOrder
- * Change the order of locales, to enable negotiation of a locale with setLocale
- * @param {String} locale The locale that should be set
- */
- eR.localeOrder = function () {
- eR.preferredLocales = R.slice(arguments);
- return eR.setLocale(eR.lang);
- };
-
- /*
- * R
- * Set the `locale` to a different locale
- * @param {String} locale The locale that should be set
- */
- eR.setLocale = function (locale, force) {
- //If we haven't initialised our variables etc, then do so.
- if (!inited) R.init();
-
- if (force) {
- eR.lang = locale;
- return eR;
- }
- var i = 0, key, locales = eR.preferredLocales;
- if (!locales) {
- locales = [];
- for (key in eR.locales) {
- if (eR.locales.hasOwnProperty(key)) locales.push(key);
- }
- }
- if ((key = R.AinOf(locales, locale)) > 0) {
- eR.lang = locales[key];
- return eR;
- }
- eR.lang = locales[0] || locale;
- return eR;
- };
-
- eR.noConflict = function () {
- exports.R = oldR;
- return eR;
- };
-
- exports.R = eR;
-
-}(typeof module !== 'undefined' && module.exports ? module.exports : this));
-});
Oops, something went wrong.
@@ -666,14 +666,14 @@ newBuiltin('listval',[TList,TNum],'?', null, {
var index = util.wrapListIndex(args[1].value,list.vars);
if(list.type!='list') {
if(list.type=='name')
- throw(new Numbas.Error('jme.variables.variable not defined',list.name));
+ throw(new Numbas.Error('jme.variables.variable not defined',{name:list.name}));
else
throw(new Numbas.Error('jme.func.listval.not a list'));
}
if(index in list.value)
return list.value[index];
else
- throw(new Numbas.Error('jme.func.listval.invalid index',index,list.value.length));
+ throw(new Numbas.Error('jme.func.listval.invalid index',{index:index,size:list.value.length}));
},
doc: {
@@ -830,7 +830,7 @@ newBuiltin('map',['?',TName,'?'],TList, null, {
var value = jme.evaluate(args[2],scope);
if(!(value.type in jme.mapFunctions)) {
- throw(new Numbas.Error('jme.typecheck.map not on enumerable',value.type));
+ throw(new Numbas.Error('jme.typecheck.map not on enumerable',{type:value.type}));
}
scope = new Scope(scope);
@@ -1166,7 +1166,7 @@ newBuiltin('matrix',[TList],TMatrix,null, {
}
break;
default:
- throw(new Numbas.Error('jme.func.matrix.invalid row type',list.value[0].type));
+ throw(new Numbas.Error('jme.func.matrix.invalid row type',{type:list.value[0].type}));
}
value.rows = rows;
value.columns = columns;
@@ -1008,7 +1008,7 @@ var texify = Numbas.jme.display.texify = function(thing,settings)
if(tok.type in typeToTeX) {
return typeToTeX[tok.type](thing,tok,texArgs,settings);
} else {
- throw(new Numbas.Error(R('jme.display.unknown token type',tok.type)));
+ throw(new Numbas.Error(R('jme.display.unknown token type',{type:tok.type})));
}
}
@@ -1367,7 +1367,7 @@ var treeToJME = jme.display.treeToJME = function(tree,settings)
if(tok.type in typeToJME) {
return typeToJME[tok.type](tree,tok,bits,settings);
} else {
- throw(new Numbas.Error(R('jme.display.unknown token type',tok.type)));
+ throw(new Numbas.Error(R('jme.display.unknown token type',{type:tok.type})));
}
}
Oops, something went wrong.

0 comments on commit ad88a15

Please sign in to comment.