improvements supplier translations

This commit is contained in:
2013-01-10 20:56:30 +01:00
parent 4d0d1b7dd1
commit f6b28f84f0
7 changed files with 113 additions and 14 deletions
@@ -32,6 +32,10 @@
//= require qwaiter
//= require_directory .
//= require_self
var Qstorage = localStorage;
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
var path_mapping = {
user_root: '/user',
join_occupied_table: '/user/join_occupied_table',
@@ -49,3 +53,15 @@ function redirect_to(mapping, variables){
function currency(num) {
return Qwaiter.currency(num);
}
$(function(){
$locale = Qstorage.getItem('locale') || 'en';
if(Qstorage.getItem('message')){
var container = $('.alert-success');
var msg_finder = Qstorage.getItem('message');
if(msg_finder.indexOf('.') == -1) msg_finder = 'messages.'+msg_finder;
container.find('div').text(t(msg_finder));
container.show();
Qstorage.removeItem('message');
}
setTranslations();
});
@@ -0,0 +1,62 @@
var $translations = {
en: {
messages: <%= I18n.t('messages', locale: :en).to_json %>,
models: <%= I18n.t('activemodel.models', locale: :en).to_json %>,
attributes: <%= I18n.t('activemodel.attributes', locale: :en).to_json %>,
<%= I18n.t('supplier', locale: :en).to_json[1..-2] %>
},
nl: {
models: <%= I18n.t('activemodel.models', locale: :nl).to_json %>,
attributes: <%= I18n.t('activemodel.attributes', locale: :nl).to_json %>,
<%= I18n.t('supplier', locale: :nl).to_json[1..-2] %>
}
}
$transformation_mappings = {
downcase: 'toLowerCase',
upcase: 'toUpperCase'
}
function t(path, vars){
vars || (vars = {});
var result, m, translatable, isafety,replacable;
var parts = path.split('.');
var accessor = '$translations.'+$locale+'["' + parts.join('"]["')+ '"]';
try{
result = eval(accessor);
} catch(err){
result = parts[parts.length - 1].capitalize();
}
if(!result) return parts[parts.length - 1].capitalize();
$.each(vars, function(v, value){ result = result.replace('%{'+v+'}', value)});
isafety = 0;
while(result.indexOf('${') > -1){
m = result.match(/\${([\w\.]+(\|\w+)?)}/);
if(m[2]){
translatable = m[1].replace(m[2], '');
operation = $transformation_mappings[m[2].substr(1) || m[2].substr(1)];
}else{
translatable = m[1];
operation = null;
}
replacable = t(translatable);
if(operation) replacable = replacable[operation]();
result = result.replace(m[0], replacable);
if(isafety > 10) break;
isafety += 1;
}
return result;
}
function setLocale(locale){
Qstorage.setItem('locale', locale);
$locale = locale;
setTranslations();
}
function setTranslations(selector){
var list = $('#top-navigation-list');
list.find('.locale').show();
list.find('.locale-'+$locale).hide();
if(selector){
$(selector).find('[data-t]').each(function(){$(this).text(t($(this).data('t'), $(this).data('tAttributes')))})
}else{
$('[data-t]').each(function(){$(this).text(t($(this).data('t'),$(this).data('tAttributes')))})
}
}