diff --git a/nailgun/static/css/main.css b/nailgun/static/css/main.css index 441e0e84ab..73fe298678 100755 --- a/nailgun/static/css/main.css +++ b/nailgun/static/css/main.css @@ -84,6 +84,14 @@ h6.page-title { width: 1030px; } +.footer-lang { + display: block; + padding: 14px 0px 0px 5px; + color: #7789a0; + font-size: 11px; + margin-top: 0px; +} + /* Navigation */ .navigation-bar { margin-top: -1px; @@ -329,6 +337,10 @@ h6.page-title { margin-top: 0px; } +.footer-lang { + margin-right: 20px; +} + .roles-action-link { padding-top: 8px; } diff --git a/nailgun/static/i18n/translation.json b/nailgun/static/i18n/translation.json new file mode 100644 index 0000000000..224301c418 --- /dev/null +++ b/nailgun/static/i18n/translation.json @@ -0,0 +1,344 @@ +{ + "en-US": { + "translation": { + "navbar": { + "stats": { + "total": ["Total", "node"], + "total_plural": ["Total", "nodes"], + "unallocated": ["Unallocated", "node"], + "unallocated_plural": ["Unallocated", "nodes"] + } + } + } + }, + "zh-CN": { + "translation": { + "navbar": { + "Environments": "环境", + "Releases": "版本", + "Support": "支持", + "stats": { + "total": "全部节点", + "unallocated": "未分配节点" + } + }, + "breadcrumb": { + "Home": "首页", + "Environments": "环境", + "Releases": "版本", + "Support": "支持", + "Notifications": "通知" + }, + "notifications_page": { + "title": "通知" + }, + "notifications_popover": { + "view_all": "查看全部", + "no_notifications_text": "没有通知可显示" + }, + "support_page": { + "title": "支持", + "contact_text": "欢迎提出反馈和改进意见,请点击下面的按钮,我们将尽快回复您。", + "irc_text": "如果您有任何问题,可通过IRC(freenode.net网络的 #fuel 频道)联系开发者。", + "log_text": "如果您在使用Fuel Web的过程中遇到任何错误或异常,可能需要将日志提交给支持团队,请点击按钮下载:", + "contact_support": "联系支持", + "download_logs": "下载日志" + }, + "release_page": { + "title": "版本", + "name": "名称", + "version": "发布", + "status": "状态", + "actions": "操作", + "alert": "没有版本可用", + "configure": "配置", + "active": "活动", + "error": "错误", + "not_available": "不可用", + "downloading": "下载中", + "progress": "下载:" + }, + "clusters_page": { + "title":"我的OpenStack环境", + "create-cluster-text": "新建OpenStack环境", + "nodes": "节点:", + "stat_new": "新建", + "stat_deploying": "部署中", + "stat_operational": "可操作", + "stat_error": "错误", + "stat_removing": "移除中" + }, + "cluster_page": { + "deploy_changes": "部署变更", + "deployment_mode": "部署模式:", + "openstack_release": "OpenStack版本:", + "environment_status": "环境状态:", + "mode_singlenode": "单节点", + "mode_multinode": "多节点", + "mode_multinode_with_HA": "HA多节点", + "type_compute_and_cinder": "计算和存储", + "type_compute_only": "仅计算", + "type_cinder_only": "仅存储", + "add": "增加", + "delete": "删除", + "controllers": "控制器", + "computes": "计算", + "cinders": "存储", + "no_nodes_text": "该类型的节点不存在", + "error": "错误", + "success": "成功", + + "network_tab": { + "title": "网络设置", + "flatdhcp_manager": "FlatDHCP管理", + "vlan_manager": "VLAN管理", + "bt_verify_networks": "验证网络", + "bt_cancel_changes": "取消变更", + "bt_save_settings": "保存设置", + "step_0": "完成网络验证3个步骤:", + "step_1": "1. 每个节点启动测试监听帧", + "step_2": "2. 每个节点发送802.1Q封装的UDP帧", + "step_3": "3. 每个节点从其它节点注册测试帧", + "ip_range": "IP范围", + "netmask": "网络掩码", + "vlan_id": "VLAN ID", + "gateway": "网关", + "cidr": "CIDR", + "nubmer_of_networks": "网络数量", + "size_of_networks": "网络大小", + "vlan_id_range": "VLAN ID范围", + "use_vlan_tagging": "使用VLAN标记", + "start": "开始", + "end": "结束", + "vm_networks_fixed": "虚机网络(固定)", + "public": "公开", + "floating": "浮动", + "management": "管理", + "storage": "存储", + "alert_success": "验证成功。网络配置正确。" + }, + + "settings_tab": { + "title": "OpenStack设置", + "bt_load_defaults": "加载默认配置", + "bt_cancel_changes": "取消变更", + "bt_save_settings": "保存设置", + "access": "访问", + "syslog": "系统日志", + "common": "通用", + "username": "用户名", + "password": "密码", + "tenant": "tenant", + "email": "电子邮件", + "Hostname": "主机名", + "Port": "端口", + "Syslog transport protocol": "系统日志传输协议", + "Hypervisor type": "管理器类型", + "Scheduler driver": "调度器驱动", + "Filter scheduler": "过滤器调度器", + "Simple scheduler": "简单调度器", + "Use qcow format for images": "为映像使用qcow格式", + "Auto assign floating IP": "自动分配浮动IP", + "Start guests on host boot": "主机启动时运行客户机", + "Public Key": "公开密钥", + "Username for Administrator": "管理员用户名", + "Password for Administrator": "管理员密码", + "Tenant (project) name for Administrator": "管理员Tenant(项目)名称", + "Email address for Administrator": "管理员电子邮件", + "Remote syslog hostname": "远程系统日志主机名", + "Remote syslog port": "远程系统日志端口", + "Choose this type of hypervisor if you run OpenStack on hardware": "如果在硬件上运行OpenStack,选择该类型的管理器", + "Choose this type of hypervisor if you run OpenStack on virtual hosts.": "如果在虚拟机上运行OpenStack,选择该类型的管理器" + }, + + "logs_tab": { + "title": "日志", + "logs": "日志", + "node": "节点", + "source": "来源", + "level": "级别", + "show": "显示", + "alert_log": "无法获取日志", + "alert_source": "无法获取日志来源", + "date": "日期", + "message": "消息", + "bottom_text": "前面的一些行被忽略。显示更多行:", + "all": "全部", + "no_log_text": "当前过滤器没有找到任何日志。" + }, + + "healthcheck_tab": { + "title": "OpenStack健康检查", + "select_all": "选择全部", + "bt_run_tests": "运行测试", + "bt_stop_tests": "停止测试", + "alert_deploy": "在测试之前必须先部署新的OpenStack环境", + "alert_not_available": "OSTF服务不可用" + }, + + "actions_tab": { + "title": "动作", + "rename_environment": "重命名环境", + "delete_environment": "删除环境", + "bt_rename": "重命名", + "bt_delete": "删除", + "alert_delete": "清除每个节点并放回未分配节点池" + }, + + "nodes_tab": { + "add": "增加", + "delete": "删除", + "controller": "控制器", + "compute": "计算", + "cinder": "存储", + "nodes": "节点", + "apply": "应用", + "cancel": "取消", + "discovered": "在线", + "offline": "离线", + "error": "错误", + "select_all": "选择全部", + "view_logs": "查看日志", + "pending_addition": "等待增加", + "pending_deletion": "等待删除", + "ready": "已就绪", + "installing": "正在安装", + "is_installed": "已安装", + "installing_openstack": "正在安装OpenStack", + "bt_return": "返回节点列表", + "bt_load_defaults": "恢复默认", + "bt_cancel_changes": "取消变更", + "bt_apply": "应用", + "configure": "配置", + "node_interfaces": "节点 接口", + "node_disks": "节点 磁盘", + "speed": "速度:", + "public": "公共", + "floating": "浮动", + "storage": "存储", + "management": "管理", + "fixed": "固定", + "alert_drag_and_drop": "使用拖拽在物理接口之间移动逻辑网络", + "total_space": "全部空间:", + "unallocated": "未分配", + "disk_information": "磁盘信息", + "volume_groups": "卷组", + "use_all_allowed_space": "使用全部可用空间" + } + }, + "dialog": { + "dismiss_settings": { + "title": "确认", + "important": "重要", + "bt_stay": "留在本页", + "bt_leave": "离开并放弃变更", + "default_msg": "设置变更未保存,您想放弃变更并离开么?", + "verify_msg": "正在进行网络验证,您应该保存变更或留在本页" + }, + + "rhel_license": { + "title": "红帽企业版Linux账户设置", + "alert_text": "安装红帽版的OpenStack平台, 你首先必须下载RHOS和RHEL并缓存在Fuel的主节点上。请选择你的订阅并在下面输入你的凭据开始下载。", + "bt_cancel": "取消", + "redhat_username": "红帽账户", + "redhat_password": "红帽密码", + "satellite_server_hostname": "satellite服务器主机名", + "activation_key": "激活码" + }, + + "create_cluster": { + "title": "新建OpenStack环境", + "name": "名称", + "openstack_release": "OpenStack版本", + "alert_text": "在部署红帽的openstack时,首先必须先下载RHOS和RHEL并缓存在Fuel的主节点上。你可以通过版本页面或则选择你的订阅类型并输入你的凭证。RHEL和RHOS将自动下载并缓存到你的创建的环境中。", + "bt_cancel": "取消", + "bt_create": "创建" + }, + + "remove_cluster": { + "title": "删除环境", + "important": "重要", + "bt_cancel": "取消", + "bt_delete": "删除", + "text1": "有动作未完成,删除环境可能失败,并导致状态不混乱。", + "text2": "每个节点都将被删除并放回未非配节点池。" + }, + + "change_cluster_mode": { + "title": "变更环境模式", + "deployment_mode": "部署模式:", + "single_node": "单节点", + "multi_node": "多节点", + "multi_node_with_ha": "HA多节点", + "single_node_text": "如果你想试用OpenStack,这个是一个简单的配置。你将获得一个安装OpenStack所有组件的功能齐全的私有云。这个节点同时也包含计算节点的角色。", + "multi_node_text": "在这个配置中,OpenStack控制节点部署从计算节点和存储节点分离出来。此模式假定有一个控制节点,一个或多个计算和存储节点存在。你可以添加更多的节点来扩展你的云。", + "ha_text": "这个配置需要多个OpenStack控制节点(3+)和所有OpenStack高可用组件,包括MySQL/Galera,RabbitMQ,Cinder,以及OpenStack的API服务。如果想建立一个生产级或者更多节点的OpenStack云,请选择这个配置。", + "bt_cancel": "取消", + "bt_apply": "应用" + }, + + "show_node": { + "label_manufacturer": "设备:", + "label_mac_address": "MAC地址:", + "system": "系统", + "memory": "内存", + "disks": "磁盘", + "interfaces": "接口", + "bt_close": "关闭", + "bt_network_configuration": "网络配置", + "bt_disk_configuration": "磁盘配置", + "product": "产品", + "family": "系列", + "version": "版本", + "serial": "序列号", + "manufacturer": "设备", + "real": "物理", + "total": "全部", + "model": "模块", + "frequency": "频率", + "slots": "插槽", + "maximum capacity": "最大负载", + "type": "类型", + "size": "大小", + "name": "名称", + "disk": "磁盘", + "max speed": "最大速度", + "current speed": "当前速度", + "netmask": "网络掩码" + }, + + "discard_changes": { + "title": "放弃变更", + "added": "增加:", + "deleted": "删除:", + "node": "节点", + "controller": "控制", + "compute": "计算", + "cinder": "存储", + "bt_cancel": "取消", + "bt_discard": "放弃", + "alert_text": "确定要放弃所有变更吗?" + }, + + "display_changes": { + "title": "部署变更", + "added": "增加:", + "node": "节点", + "controller": "控制", + "compute": "计算", + "cinder": "存储", + "changed": "变更:", + "deleted": "删除:", + "bt_cancel": "取消", + "bt_deploy": "部署", + "openstack_settings": "OpenStack设置", + "network_settings": "网络设置", + "disks_text": "以下节点的磁盘配置变更:", + "alert_text": "一些节点状态错误,需要重新部署。", + "warning_controller": "部署后的环境将不包含足够数量的控制器,也许不可使用。", + "warning_compute": "建议部署计算节点。" + } + } + } + } +} diff --git a/nailgun/static/js/libs/i18next.amd.withJQuery-1.6.3.js b/nailgun/static/js/libs/i18next.amd.withJQuery-1.6.3.js new file mode 100644 index 0000000000..96e9a220d4 --- /dev/null +++ b/nailgun/static/js/libs/i18next.amd.withJQuery-1.6.3.js @@ -0,0 +1,2615 @@ +// i18next, v1.6.3 +// Copyright (c)2013 Jan Mühlemann (jamuhl). +// Distributed under MIT license +// http://i18next.com +(function (root, factory) { + if (typeof exports === 'object') { + + var jquery = require('jquery'); + + module.exports = factory(jquery); + + } else if (typeof define === 'function' && define.amd) { + + define(['jquery'], factory); + + } +}(this, function ($) { + + // add indexOf to non ECMA-262 standard compliant browsers + if (!Array.prototype.indexOf) { + Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) { + "use strict"; + if (this == null) { + throw new TypeError(); + } + var t = Object(this); + var len = t.length >>> 0; + if (len === 0) { + return -1; + } + var n = 0; + if (arguments.length > 0) { + n = Number(arguments[1]); + if (n != n) { // shortcut for verifying if it's NaN + n = 0; + } else if (n != 0 && n != Infinity && n != -Infinity) { + n = (n > 0 || -1) * Math.floor(Math.abs(n)); + } + } + if (n >= len) { + return -1; + } + var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0); + for (; k < len; k++) { + if (k in t && t[k] === searchElement) { + return k; + } + } + return -1; + } + } + + // add lastIndexOf to non ECMA-262 standard compliant browsers + if (!Array.prototype.lastIndexOf) { + Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) { + "use strict"; + if (this == null) { + throw new TypeError(); + } + var t = Object(this); + var len = t.length >>> 0; + if (len === 0) { + return -1; + } + var n = len; + if (arguments.length > 1) { + n = Number(arguments[1]); + if (n != n) { + n = 0; + } else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) { + n = (n > 0 || -1) * Math.floor(Math.abs(n)); + } + } + var k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); + for (; k >= 0; k--) { + if (k in t && t[k] === searchElement) { + return k; + } + } + return -1; + }; + } + + var i18n = {} + , resStore = {} + , currentLng + , replacementCounter = 0 + , languages = []; + + // defaults + var o = { + lng: undefined, + load: 'all', + preload: [], + lowerCaseLng: false, + returnObjectTrees: false, + fallbackLng: 'dev', + fallbackNS: [], + detectLngQS: 'setLng', + ns: 'translation', + fallbackOnNull: true, + fallbackToDefaultNS: false, + nsseparator: ':', + keyseparator: '.', + selectorAttr: 'data-i18n', + debug: false, + + resGetPath: 'locales/__lng__/__ns__.json', + resPostPath: 'locales/add/__lng__/__ns__', + + getAsync: true, + postAsync: true, + + resStore: undefined, + useLocalStorage: false, + localStorageExpirationTime: 7*24*60*60*1000, + + dynamicLoad: false, + sendMissing: false, + sendMissingTo: 'fallback', // current | all + sendType: 'POST', + + interpolationPrefix: '__', + interpolationSuffix: '__', + reusePrefix: '$t(', + reuseSuffix: ')', + pluralSuffix: '_plural', + pluralNotFound: ['plural_not_found', Math.random()].join(''), + contextNotFound: ['context_not_found', Math.random()].join(''), + escapeInterpolation: false, + + setJqueryExt: true, + defaultValueFromContent: true, + useDataAttrOptions: false, + cookieExpirationTime: undefined, + useCookie: true, + cookieName: 'i18next', + + postProcess: undefined, + parseMissingKey: undefined + }; + function _extend(target, source) { + if (!source || typeof source === 'function') { + return target; + } + + for (var attr in source) { target[attr] = source[attr]; } + return target; + } + + function _each(object, callback, args) { + var name, i = 0, + length = object.length, + isObj = length === undefined || typeof object === "function"; + + if (args) { + if (isObj) { + for (name in object) { + if (callback.apply(object[name], args) === false) { + break; + } + } + } else { + for ( ; i < length; ) { + if (callback.apply(object[i++], args) === false) { + break; + } + } + } + + // A special, fast, case for the most common use of each + } else { + if (isObj) { + for (name in object) { + if (callback.call(object[name], name, object[name]) === false) { + break; + } + } + } else { + for ( ; i < length; ) { + if (callback.call(object[i], i, object[i++]) === false) { + break; + } + } + } + } + + return object; + } + + var _entityMap = { + "&": "&", + "<": "<", + ">": ">", + '"': '"', + "'": ''', + "/": '/' + }; + + function _escape(data) { + if (typeof data === 'string') { + return data.replace(/[&<>"'\/]/g, function (s) { + return _entityMap[s]; + }); + }else{ + return data; + } + } + + function _ajax(options) { + + // v0.5.0 of https://github.com/goloroden/http.js + var getXhr = function (callback) { + // Use the native XHR object if the browser supports it. + if (window.XMLHttpRequest) { + return callback(null, new XMLHttpRequest()); + } else if (window.ActiveXObject) { + // In Internet Explorer check for ActiveX versions of the XHR object. + try { + return callback(null, new ActiveXObject("Msxml2.XMLHTTP")); + } catch (e) { + return callback(null, new ActiveXObject("Microsoft.XMLHTTP")); + } + } + + // If no XHR support was found, throw an error. + return callback(new Error()); + }; + + var encodeUsingUrlEncoding = function (data) { + if(typeof data === 'string') { + return data; + } + + var result = []; + for(var dataItem in data) { + if(data.hasOwnProperty(dataItem)) { + result.push(encodeURIComponent(dataItem) + '=' + encodeURIComponent(data[dataItem])); + } + } + + return result.join('&'); + }; + + var utf8 = function (text) { + text = text.replace(/\r\n/g, '\n'); + var result = ''; + + for(var i = 0; i < text.length; i++) { + var c = text.charCodeAt(i); + + if(c < 128) { + result += String.fromCharCode(c); + } else if((c > 127) && (c < 2048)) { + result += String.fromCharCode((c >> 6) | 192); + result += String.fromCharCode((c & 63) | 128); + } else { + result += String.fromCharCode((c >> 12) | 224); + result += String.fromCharCode(((c >> 6) & 63) | 128); + result += String.fromCharCode((c & 63) | 128); + } + } + + return result; + }; + + var base64 = function (text) { + var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; + + text = utf8(text); + var result = '', + chr1, chr2, chr3, + enc1, enc2, enc3, enc4, + i = 0; + + do { + chr1 = text.charCodeAt(i++); + chr2 = text.charCodeAt(i++); + chr3 = text.charCodeAt(i++); + + enc1 = chr1 >> 2; + enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); + enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); + enc4 = chr3 & 63; + + if(isNaN(chr2)) { + enc3 = enc4 = 64; + } else if(isNaN(chr3)) { + enc4 = 64; + } + + result += + keyStr.charAt(enc1) + + keyStr.charAt(enc2) + + keyStr.charAt(enc3) + + keyStr.charAt(enc4); + chr1 = chr2 = chr3 = ''; + enc1 = enc2 = enc3 = enc4 = ''; + } while(i < text.length); + + return result; + }; + + var mergeHeaders = function () { + // Use the first header object as base. + var result = arguments[0]; + + // Iterate through the remaining header objects and add them. + for(var i = 1; i < arguments.length; i++) { + var currentHeaders = arguments[i]; + for(var header in currentHeaders) { + if(currentHeaders.hasOwnProperty(header)) { + result[header] = currentHeaders[header]; + } + } + } + + // Return the merged headers. + return result; + }; + + var ajax = function (method, url, options, callback) { + // Adjust parameters. + if(typeof options === 'function') { + callback = options; + options = {}; + } + + // Set default parameter values. + options.cache = options.cache || false; + options.data = options.data || {}; + options.headers = options.headers || {}; + options.jsonp = options.jsonp || false; + options.async = options.async === undefined ? true : options.async; + + // Merge the various header objects. + var headers = mergeHeaders({ + 'accept': '*/*', + 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' + }, ajax.headers, options.headers); + + // Encode the data according to the content-type. + var payload; + if (headers['content-type'] === 'application/json') { + payload = JSON.stringify(options.data); + } else { + payload = encodeUsingUrlEncoding(options.data); + } + + // Specially prepare GET requests: Setup the query string, handle caching and make a JSONP call + // if neccessary. + if(method === 'GET') { + // Setup the query string. + var queryString = []; + if(payload) { + queryString.push(payload); + payload = null; + } + + // Handle caching. + if(!options.cache) { + queryString.push('_=' + (new Date()).getTime()); + } + + // If neccessary prepare the query string for a JSONP call. + if(options.jsonp) { + queryString.push('callback=' + options.jsonp); + queryString.push('jsonp=' + options.jsonp); + } + + // Merge the query string and attach it to the url. + queryString = queryString.join('&'); + if (queryString.length > 1) { + if (url.indexOf('?') > -1) { + url += '&' + queryString; + } else { + url += '?' + queryString; + } + } + + // Make a JSONP call if neccessary. + if(options.jsonp) { + var head = document.getElementsByTagName('head')[0]; + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = url; + head.appendChild(script); + return; + } + } + + // Since we got here, it is no JSONP request, so make a normal XHR request. + getXhr(function (err, xhr) { + if(err) return callback(err); + + // Open the request. + xhr.open(method, url, options.async); + + // Set the request headers. + for(var header in headers) { + if(headers.hasOwnProperty(header)) { + xhr.setRequestHeader(header, headers[header]); + } + } + + // Handle the request events. + xhr.onreadystatechange = function () { + if(xhr.readyState === 4) { + var data = xhr.responseText || ''; + + // If no callback is given, return. + if(!callback) { + return; + } + + // Return an object that provides access to the data as text and JSON. + callback(xhr.status, { + text: function () { + return data; + }, + + json: function () { + return JSON.parse(data); + } + }); + } + }; + + // Actually send the XHR request. + xhr.send(payload); + }); + }; + + // Define the external interface. + var http = { + authBasic: function (username, password) { + ajax.headers['Authorization'] = 'Basic ' + base64(username + ':' + password); + }, + + connect: function (url, options, callback) { + return ajax('CONNECT', url, options, callback); + }, + + del: function (url, options, callback) { + return ajax('DELETE', url, options, callback); + }, + + get: function (url, options, callback) { + return ajax('GET', url, options, callback); + }, + + head: function (url, options, callback) { + return ajax('HEAD', url, options, callback); + }, + + headers: function (headers) { + ajax.headers = headers || {}; + }, + + isAllowed: function (url, verb, callback) { + this.options(url, function (status, data) { + callback(data.text().indexOf(verb) !== -1); + }); + }, + + options: function (url, options, callback) { + return ajax('OPTIONS', url, options, callback); + }, + + patch: function (url, options, callback) { + return ajax('PATCH', url, options, callback); + }, + + post: function (url, options, callback) { + return ajax('POST', url, options, callback); + }, + + put: function (url, options, callback) { + return ajax('PUT', url, options, callback); + }, + + trace: function (url, options, callback) { + return ajax('TRACE', url, options, callback); + } + }; + + + var methode = options.type ? options.type.toLowerCase() : 'get'; + + http[methode](options.url, options, function (status, data) { + if (status === 200) { + options.success(data.json(), status, null); + } else { + options.error(data.text(), status, null); + } + }); + } + + var _cookie = { + create: function(name,value,minutes) { + var expires; + if (minutes) { + var date = new Date(); + date.setTime(date.getTime()+(minutes*60*1000)); + expires = "; expires="+date.toGMTString(); + } + else expires = ""; + document.cookie = name+"="+value+expires+"; path=/"; + }, + + read: function(name) { + var nameEQ = name + "="; + var ca = document.cookie.split(';'); + for(var i=0;i < ca.length;i++) { + var c = ca[i]; + while (c.charAt(0)==' ') c = c.substring(1,c.length); + if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length); + } + return null; + }, + + remove: function(name) { + this.create(name,"",-1); + } + }; + + var cookie_noop = { + create: function(name,value,minutes) {}, + read: function(name) { return null; }, + remove: function(name) {} + }; + + + + // move dependent functions to a container so that + // they can be overriden easier in no jquery environment (node.js) + var f = { + extend: $ ? $.extend : _extend, + each: $ ? $.each : _each, + ajax: $ ? $.ajax : _ajax, + cookie: typeof document !== 'undefined' ? _cookie : cookie_noop, + detectLanguage: detectLanguage, + escape: _escape, + log: function(str) { + if (o.debug && typeof console !== "undefined") console.log(str); + }, + toLanguages: function(lng) { + var languages = []; + if (typeof lng === 'string' && lng.indexOf('-') > -1) { + var parts = lng.split('-'); + + lng = o.lowerCaseLng ? + parts[0].toLowerCase() + '-' + parts[1].toLowerCase() : + parts[0].toLowerCase() + '-' + parts[1].toUpperCase(); + + if (o.load !== 'unspecific') languages.push(lng); + if (o.load !== 'current') languages.push(parts[0]); + } else { + languages.push(lng); + } + + if (languages.indexOf(o.fallbackLng) === -1 && o.fallbackLng) languages.push(o.fallbackLng); + + return languages; + }, + regexEscape: function(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); + } + }; + function init(options, cb) { + + if (typeof options === 'function') { + cb = options; + options = {}; + } + options = options || {}; + + // override defaults with passed in options + f.extend(o, options); + + // create namespace object if namespace is passed in as string + if (typeof o.ns == 'string') { + o.ns = { namespaces: [o.ns], defaultNs: o.ns}; + } + + // fallback namespaces + if (typeof o.fallbackNS == 'string') { + o.fallbackNS = [o.fallbackNS]; + } + + // escape prefix/suffix + o.interpolationPrefixEscaped = f.regexEscape(o.interpolationPrefix); + o.interpolationSuffixEscaped = f.regexEscape(o.interpolationSuffix); + + if (!o.lng) o.lng = f.detectLanguage(); + if (o.lng) { + // set cookie with lng set (as detectLanguage will set cookie on need) + if (o.useCookie) f.cookie.create(o.cookieName, o.lng, o.cookieExpirationTime); + } else { + o.lng = o.fallbackLng; + if (o.useCookie) f.cookie.remove(o.cookieName); + } + + languages = f.toLanguages(o.lng); + currentLng = languages[0]; + f.log('currentLng set to: ' + currentLng); + + pluralExtensions.setCurrentLng(currentLng); + + // add JQuery extensions + if ($ && o.setJqueryExt) addJqueryFunct(); + + // jQuery deferred + var deferred; + if ($ && $.Deferred) { + deferred = $.Deferred(); + } + + // return immidiatly if res are passed in + if (o.resStore) { + resStore = o.resStore; + if (cb) cb(translate); + if (deferred) deferred.resolve(); + if (deferred) return deferred.promise(); + return; + } + + // languages to load + var lngsToLoad = f.toLanguages(o.lng); + if (typeof o.preload === 'string') o.preload = [o.preload]; + for (var i = 0, l = o.preload.length; i < l; i++) { + var pres = f.toLanguages(o.preload[i]); + for (var y = 0, len = pres.length; y < len; y++) { + if (lngsToLoad.indexOf(pres[y]) < 0) { + lngsToLoad.push(pres[y]); + } + } + } + + // else load them + i18n.sync.load(lngsToLoad, o, function(err, store) { + resStore = store; + + if (cb) cb(translate); + if (deferred) deferred.resolve(); + }); + + if (deferred) return deferred.promise(); + } + function preload(lngs, cb) { + if (typeof lngs === 'string') lngs = [lngs]; + for (var i = 0, l = lngs.length; i < l; i++) { + if (o.preload.indexOf(lngs[i]) < 0) { + o.preload.push(lngs[i]); + } + } + return init(cb); + } + + function addResourceBundle(lng, ns, resources) { + if (typeof ns !== 'string') { + resources = ns; + ns = o.ns.defaultNs; + } else if (o.ns.namespaces.indexOf(ns) < 0) { + o.ns.namespaces.push(ns); + } + + resStore[lng] = resStore[lng] || {}; + resStore[lng][ns] = resStore[lng][ns] || {}; + + f.extend(resStore[lng][ns], resources); + } + + function setDefaultNamespace(ns) { + o.ns.defaultNs = ns; + } + + function loadNamespace(namespace, cb) { + loadNamespaces([namespace], cb); + } + + function loadNamespaces(namespaces, cb) { + var opts = { + dynamicLoad: o.dynamicLoad, + resGetPath: o.resGetPath, + getAsync: o.getAsync, + customLoad: o.customLoad, + ns: { namespaces: namespaces, defaultNs: ''} /* new namespaces to load */ + }; + + // languages to load + var lngsToLoad = f.toLanguages(o.lng); + if (typeof o.preload === 'string') o.preload = [o.preload]; + for (var i = 0, l = o.preload.length; i < l; i++) { + var pres = f.toLanguages(o.preload[i]); + for (var y = 0, len = pres.length; y < len; y++) { + if (lngsToLoad.indexOf(pres[y]) < 0) { + lngsToLoad.push(pres[y]); + } + } + } + + // check if we have to load + var lngNeedLoad = []; + for (var a = 0, lenA = lngsToLoad.length; a < lenA; a++) { + var needLoad = false; + var resSet = resStore[lngsToLoad[a]]; + if (resSet) { + for (var b = 0, lenB = namespaces.length; b < lenB; b++) { + if (!resSet[namespaces[b]]) needLoad = true; + } + } else { + needLoad = true; + } + + if (needLoad) lngNeedLoad.push(lngsToLoad[a]); + } + + if (lngNeedLoad.length) { + i18n.sync._fetch(lngNeedLoad, opts, function(err, store) { + var todo = namespaces.length * lngNeedLoad.length; + + // load each file individual + f.each(namespaces, function(nsIndex, nsValue) { + + // append namespace to namespace array + if (o.ns.namespaces.indexOf(nsValue) < 0) { + o.ns.namespaces.push(nsValue); + } + + f.each(lngNeedLoad, function(lngIndex, lngValue) { + resStore[lngValue] = resStore[lngValue] || {}; + resStore[lngValue][nsValue] = store[lngValue][nsValue]; + + todo--; // wait for all done befor callback + if (todo === 0 && cb) { + if (o.useLocalStorage) i18n.sync._storeLocal(resStore); + cb(); + } + }); + }); + }); + } else { + if (cb) cb(); + } + } + + function setLng(lng, cb) { + return init({lng: lng}, cb); + } + + function lng() { + return currentLng; + } + function addJqueryFunct() { + // $.t shortcut + $.t = $.t || translate; + + function parse(ele, key, options) { + if (key.length === 0) return; + + var attr = 'text'; + + if (key.indexOf('[') === 0) { + var parts = key.split(']'); + key = parts[1]; + attr = parts[0].substr(1, parts[0].length-1); + } + + if (key.indexOf(';') === key.length-1) { + key = key.substr(0, key.length-2); + } + + var optionsToUse; + if (attr === 'html') { + optionsToUse = o.defaultValueFromContent ? $.extend({ defaultValue: ele.html() }, options) : options; + ele.html($.t(key, optionsToUse)); + } + else if (attr === 'text') { + optionsToUse = o.defaultValueFromContent ? $.extend({ defaultValue: ele.text() }, options) : options; + ele.text($.t(key, optionsToUse)); + } else { + optionsToUse = o.defaultValueFromContent ? $.extend({ defaultValue: ele.attr(attr) }, options) : options; + ele.attr(attr, $.t(key, optionsToUse)); + } + } + + function localize(ele, options) { + var key = ele.attr(o.selectorAttr); + if (!key) return; + + var target = ele + , targetSelector = ele.data("i18n-target"); + if (targetSelector) { + target = ele.find(targetSelector) || ele; + } + + if (!options && o.useDataAttrOptions === true) { + options = ele.data("i18n-options"); + } + options = options || {}; + + if (key.indexOf(';') >= 0) { + var keys = key.split(';'); + + $.each(keys, function(m, k) { + if (k !== '') parse(target, k, options); + }); + + } else { + parse(target, key, options); + } + + if (o.useDataAttrOptions === true) ele.data("i18n-options", options); + } + + // fn + $.fn.i18n = function (options) { + return this.each(function() { + // localize element itself + localize($(this), options); + + // localize childs + var elements = $(this).find('[' + o.selectorAttr + ']'); + elements.each(function() { + localize($(this), options); + }); + }); + }; + } + function applyReplacement(str, replacementHash, nestedKey, options) { + if (!str) return str; + + options = options || replacementHash; // first call uses replacement hash combined with options + if (str.indexOf(options.interpolationPrefix || o.interpolationPrefix) < 0) return str; + + var prefix = options.interpolationPrefix ? f.regexEscape(options.interpolationPrefix) : o.interpolationPrefixEscaped + , suffix = options.interpolationSuffix ? f.regexEscape(options.interpolationSuffix) : o.interpolationSuffixEscaped + , unEscapingSuffix = 'HTML'+suffix; + + f.each(replacementHash, function(key, value) { + var nextKey = nestedKey ? nestedKey + o.keyseparator + key : key; + if (typeof value === 'object' && value !== null) { + str = applyReplacement(str, value, nextKey, options); + } else { + if (options.escapeInterpolation || o.escapeInterpolation) { + str = str.replace(new RegExp([prefix, nextKey, unEscapingSuffix].join(''), 'g'), value); + str = str.replace(new RegExp([prefix, nextKey, suffix].join(''), 'g'), f.escape(value)); + } else { + str = str.replace(new RegExp([prefix, nextKey, suffix].join(''), 'g'), value); + } + // str = options.escapeInterpolation; + } + }); + return str; + } + + // append it to functions + f.applyReplacement = applyReplacement; + + function applyReuse(translated, options) { + var comma = ','; + var options_open = '{'; + var options_close = '}'; + + var opts = f.extend({}, options); + delete opts.postProcess; + + while (translated.indexOf(o.reusePrefix) != -1) { + replacementCounter++; + if (replacementCounter > o.maxRecursion) { break; } // safety net for too much recursion + var index_of_opening = translated.lastIndexOf(o.reusePrefix); + var index_of_end_of_closing = translated.indexOf(o.reuseSuffix, index_of_opening) + o.reuseSuffix.length; + var token = translated.substring(index_of_opening, index_of_end_of_closing); + var token_without_symbols = token.replace(o.reusePrefix, '').replace(o.reuseSuffix, ''); + + + if (token_without_symbols.indexOf(comma) != -1) { + var index_of_token_end_of_closing = token_without_symbols.indexOf(comma); + if (token_without_symbols.indexOf(options_open, index_of_token_end_of_closing) != -1 && token_without_symbols.indexOf(options_close, index_of_token_end_of_closing) != -1) { + var index_of_opts_opening = token_without_symbols.indexOf(options_open, index_of_token_end_of_closing); + var index_of_opts_end_of_closing = token_without_symbols.indexOf(options_close, index_of_opts_opening) + options_close.length; + try { + opts = f.extend(opts, JSON.parse(token_without_symbols.substring(index_of_opts_opening, index_of_opts_end_of_closing))); + token_without_symbols = token_without_symbols.substring(0, index_of_token_end_of_closing); + } catch (e) { + } + } + } + + var translated_token = _translate(token_without_symbols, opts); + translated = translated.replace(token, translated_token); + } + return translated; + } + + function hasContext(options) { + return (options.context && typeof options.context == 'string'); + } + + function needsPlural(options) { + return (options.count !== undefined && typeof options.count != 'string' && options.count !== 1); + } + + function exists(key, options) { + options = options || {}; + + var notFound = options.defaultValue || key + , found = _find(key, options); + + return found !== undefined || found === notFound; + } + + function translate(key, options) { + replacementCounter = 0; + return _translate.apply(null, arguments); + } + + function _injectSprintfProcessor() { + + var values = []; + + // mh: build array from second argument onwards + for (var i = 1; i < arguments.length; i++) { + values.push(arguments[i]); + } + + return { + postProcess: 'sprintf', + sprintf: values + }; + } + + function _translate(key, options) { + + if (typeof options == 'string') { + // mh: gettext like sprintf syntax found, automatically create sprintf processor + options = _injectSprintfProcessor.apply(null, arguments); + } else { + options = options || {}; + } + + var notFound = options.defaultValue || key + , found = _find(key, options) + , lngs = options.lng ? f.toLanguages(options.lng) : languages + , ns = options.ns || o.ns.defaultNs + , parts; + + // split ns and key + if (key.indexOf(o.nsseparator) > -1) { + parts = key.split(o.nsseparator); + ns = parts[0]; + key = parts[1]; + } + + if (found === undefined && o.sendMissing) { + if (options.lng) { + sync.postMissing(lngs[0], ns, key, notFound, lngs); + } else { + sync.postMissing(o.lng, ns, key, notFound, lngs); + } + } + + var postProcessor = options.postProcess || o.postProcess; + if (found !== undefined && postProcessor) { + if (postProcessors[postProcessor]) { + found = postProcessors[postProcessor](found, key, options); + } + } + + // process notFound if function exists + var splitNotFound = notFound; + if (notFound.indexOf(o.nsseparator) > -1) { + parts = notFound.split(o.nsseparator); + splitNotFound = parts[1]; + } + if (splitNotFound === key && o.parseMissingKey) { + notFound = o.parseMissingKey(notFound); + } + + if (found === undefined) { + notFound = applyReplacement(notFound, options); + notFound = applyReuse(notFound, options); + + if (postProcessor && postProcessors[postProcessor]) { + var val = options.defaultValue || key; + found = postProcessors[postProcessor](val, key, options); + } + } + + return (found !== undefined) ? found : notFound; + } + + function _find(key, options){ + options = options || {}; + + var optionWithoutCount, translated + , notFound = options.defaultValue || key + , lngs = languages; + + if (!resStore) { return notFound; } // no resStore to translate from + + if (options.lng) { + lngs = f.toLanguages(options.lng); + + if (!resStore[lngs[0]]) { + var oldAsync = o.getAsync; + o.getAsync = false; + + i18n.sync.load(lngs, o, function(err, store) { + f.extend(resStore, store); + o.getAsync = oldAsync; + }); + } + } + + var ns = options.ns || o.ns.defaultNs; + if (key.indexOf(o.nsseparator) > -1) { + var parts = key.split(o.nsseparator); + ns = parts[0]; + key = parts[1]; + } + + if (hasContext(options)) { + optionWithoutCount = f.extend({}, options); + delete optionWithoutCount.context; + optionWithoutCount.defaultValue = o.contextNotFound; + + var contextKey = ns + o.nsseparator + key + '_' + options.context; + + translated = translate(contextKey, optionWithoutCount); + if (translated != o.contextNotFound) { + return applyReplacement(translated, { context: options.context }); // apply replacement for context only + } // else continue translation with original/nonContext key + } + + if (needsPlural(options)) { + optionWithoutCount = f.extend({}, options); + delete optionWithoutCount.count; + optionWithoutCount.defaultValue = o.pluralNotFound; + + var pluralKey = ns + o.nsseparator + key + o.pluralSuffix; + var pluralExtension = pluralExtensions.get(lngs[0], options.count); + if (pluralExtension >= 0) { + pluralKey = pluralKey + '_' + pluralExtension; + } else if (pluralExtension === 1) { + pluralKey = ns + o.nsseparator + key; // singular + } + + translated = translate(pluralKey, optionWithoutCount); + if (translated != o.pluralNotFound) { + return applyReplacement(translated, { + count: options.count, + interpolationPrefix: options.interpolationPrefix, + interpolationSuffix: options.interpolationSuffix + }); // apply replacement for count only + } // else continue translation with original/singular key + } + + var found; + var keys = key.split(o.keyseparator); + for (var i = 0, len = lngs.length; i < len; i++ ) { + if (found !== undefined) break; + + var l = lngs[i]; + + var x = 0; + var value = resStore[l] && resStore[l][ns]; + while (keys[x]) { + value = value && value[keys[x]]; + x++; + } + if (value !== undefined) { + if (typeof value === 'string') { + value = applyReplacement(value, options); + value = applyReuse(value, options); + } else if (Object.prototype.toString.apply(value) === '[object Array]' && !o.returnObjectTrees && !options.returnObjectTrees) { + value = value.join('\n'); + value = applyReplacement(value, options); + value = applyReuse(value, options); + } else if (value === null && o.fallbackOnNull === true) { + value = undefined; + } else if (value !== null) { + if (!o.returnObjectTrees && !options.returnObjectTrees) { + value = 'key \'' + ns + ':' + key + ' (' + l + ')\' ' + + 'returned a object instead of string.'; + f.log(value); + } else { + var copy = {}; // apply child translation on a copy + for (var m in value) { + // apply translation on childs + copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options); + } + value = copy; + } + } + found = value; + } + } + + if (found === undefined && !options.isFallbackLookup && (o.fallbackToDefaultNS === true || (o.fallbackNS && o.fallbackNS.length > 0))) { + // set flag for fallback lookup - avoid recursion + options.isFallbackLookup = true; + + if (o.fallbackNS.length) { + + for (var y = 0, lenY = o.fallbackNS.length; y < lenY; y++) { + found = _find(o.fallbackNS[y] + o.nsseparator + key, options); + + if (found) { + /* compare value without namespace */ + var foundValue = found.indexOf(o.nsseparator) > -1 ? found.split(o.nsseparator)[1] : found + , notFoundValue = notFound.indexOf(o.nsseparator) > -1 ? notFound.split(o.nsseparator)[1] : notFound; + + if (foundValue !== notFoundValue) break; + } + } + } else { + found = _find(key, options); // fallback to default NS + } + } + + return found; + } + function detectLanguage() { + var detectedLng; + + // get from qs + var qsParm = []; + if (typeof window !== 'undefined') { + (function() { + var query = window.location.search.substring(1); + var parms = query.split('&'); + for (var i=0; i 0) { + var key = parms[i].substring(0,pos); + var val = parms[i].substring(pos+1); + qsParm[key] = val; + } + } + })(); + if (qsParm[o.detectLngQS]) { + detectedLng = qsParm[o.detectLngQS]; + } + } + + // get from cookie + if (!detectedLng && typeof document !== 'undefined' && o.useCookie ) { + var c = f.cookie.read(o.cookieName); + if (c) detectedLng = c; + } + + // get from navigator + if (!detectedLng && typeof navigator !== 'undefined') { + detectedLng = (navigator.language) ? navigator.language : navigator.userLanguage; + } + + return detectedLng; + } + var sync = { + + load: function(lngs, options, cb) { + if (options.useLocalStorage) { + sync._loadLocal(lngs, options, function(err, store) { + var missingLngs = []; + for (var i = 0, len = lngs.length; i < len; i++) { + if (!store[lngs[i]]) missingLngs.push(lngs[i]); + } + + if (missingLngs.length > 0) { + sync._fetch(missingLngs, options, function(err, fetched) { + f.extend(store, fetched); + sync._storeLocal(fetched); + + cb(null, store); + }); + } else { + cb(null, store); + } + }); + } else { + sync._fetch(lngs, options, function(err, store){ + cb(null, store); + }); + } + }, + + _loadLocal: function(lngs, options, cb) { + var store = {} + , nowMS = new Date().getTime(); + + if(window.localStorage) { + + var todo = lngs.length; + + f.each(lngs, function(key, lng) { + var local = window.localStorage.getItem('res_' + lng); + + if (local) { + local = JSON.parse(local); + + if (local.i18nStamp && local.i18nStamp + options.localStorageExpirationTime > nowMS) { + store[lng] = local; + } + } + + todo--; // wait for all done befor callback + if (todo === 0) cb(null, store); + }); + } + }, + + _storeLocal: function(store) { + if(window.localStorage) { + for (var m in store) { + store[m].i18nStamp = new Date().getTime(); + window.localStorage.setItem('res_' + m, JSON.stringify(store[m])); + } + } + return; + }, + + _fetch: function(lngs, options, cb) { + var ns = options.ns + , store = {}; + + if (!options.dynamicLoad) { + var todo = ns.namespaces.length * lngs.length + , errors; + + // load each file individual + f.each(ns.namespaces, function(nsIndex, nsValue) { + f.each(lngs, function(lngIndex, lngValue) { + + // Call this once our translation has returned. + var loadComplete = function(err, data) { + if (err) { + errors = errors || []; + errors.push(err); + } + store[lngValue] = store[lngValue] || {}; + store[lngValue][nsValue] = data; + + todo--; // wait for all done befor callback + if (todo === 0) cb(errors, store); + }; + + if(typeof options.customLoad == 'function'){ + // Use the specified custom callback. + options.customLoad(lngValue, nsValue, options, loadComplete); + } else { + //~ // Use our inbuilt sync. + sync._fetchOne(lngValue, nsValue, options, loadComplete); + } + }); + }); + } else { + // Call this once our translation has returned. + var loadComplete = function(err, data) { + cb(null, data); + }; + + if(typeof options.customLoad == 'function'){ + // Use the specified custom callback. + options.customLoad(lngs, ns.namespaces, options, loadComplete); + } else { + var url = applyReplacement(options.resGetPath, { lng: lngs.join('+'), ns: ns.namespaces.join('+') }); + // load all needed stuff once + f.ajax({ + url: url, + success: function(data, status, xhr) { + f.log('loaded: ' + url); + loadComplete(null, data); + }, + error : function(xhr, status, error) { + f.log('failed loading: ' + url); + loadComplete('failed loading resource.json error: ' + error); + }, + dataType: "json", + async : options.getAsync + }); + } + } + }, + + _fetchOne: function(lng, ns, options, done) { + var url = applyReplacement(options.resGetPath, { lng: lng, ns: ns }); + f.ajax({ + url: url, + success: function(data, status, xhr) { + f.log('loaded: ' + url); + done(null, data); + }, + error : function(xhr, status, error) { + f.log('failed loading: ' + url); + done(error, {}); + }, + dataType: "json", + async : options.getAsync + }); + }, + + postMissing: function(lng, ns, key, defaultValue, lngs) { + var payload = {}; + payload[key] = defaultValue; + + var urls = []; + + if (o.sendMissingTo === 'fallback' && o.fallbackLng !== false) { + urls.push({lng: o.fallbackLng, url: applyReplacement(o.resPostPath, { lng: o.fallbackLng, ns: ns })}); + } else if (o.sendMissingTo === 'current' || (o.sendMissingTo === 'fallback' && o.fallbackLng === false) ) { + urls.push({lng: lng, url: applyReplacement(o.resPostPath, { lng: lng, ns: ns })}); + } else if (o.sendMissingTo === 'all') { + for (var i = 0, l = lngs.length; i < l; i++) { + urls.push({lng: lngs[i], url: applyReplacement(o.resPostPath, { lng: lngs[i], ns: ns })}); + } + } + + for (var y = 0, len = urls.length; y < len; y++) { + var item = urls[y]; + f.ajax({ + url: item.url, + type: o.sendType, + data: payload, + success: function(data, status, xhr) { + f.log('posted missing key \'' + key + '\' to: ' + item.url); + + // add key to resStore + var keys = key.split('.'); + var x = 0; + var value = resStore[item.lng][ns]; + while (keys[x]) { + if (x === keys.length - 1) { + value = value[keys[x]] = defaultValue; + } else { + value = value[keys[x]] = value[keys[x]] || {}; + } + x++; + } + }, + error : function(xhr, status, error) { + f.log('failed posting missing key \'' + key + '\' to: ' + item.url); + }, + dataType: "json", + async : o.postAsync + }); + } + } + }; + // definition http://translate.sourceforge.net/wiki/l10n/pluralforms + var pluralExtensions = { + + rules: { + "ach": { + "name": "Acholi", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "af": { + "name": "Afrikaans", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ak": { + "name": "Akan", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "am": { + "name": "Amharic", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "an": { + "name": "Aragonese", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ar": { + "name": "Arabic", + "numbers": [ + 0, + 1, + 2, + 3, + 11, + 100 + ], + "plurals": function(n) { return Number(n===0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5); } + }, + "arn": { + "name": "Mapudungun", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "ast": { + "name": "Asturian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ay": { + "name": "Aymar\u00e1", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "az": { + "name": "Azerbaijani", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "be": { + "name": "Belarusian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "bg": { + "name": "Bulgarian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "bn": { + "name": "Bengali", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "bo": { + "name": "Tibetan", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "br": { + "name": "Breton", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "bs": { + "name": "Bosnian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "ca": { + "name": "Catalan", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "cgg": { + "name": "Chiga", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "cs": { + "name": "Czech", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2); } + }, + "csb": { + "name": "Kashubian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "cy": { + "name": "Welsh", + "numbers": [ + 1, + 2, + 3, + 8 + ], + "plurals": function(n) { return Number((n==1) ? 0 : (n==2) ? 1 : (n != 8 && n != 11) ? 2 : 3); } + }, + "da": { + "name": "Danish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "de": { + "name": "German", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "dz": { + "name": "Dzongkha", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "el": { + "name": "Greek", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "en": { + "name": "English", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "eo": { + "name": "Esperanto", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "es": { + "name": "Spanish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "es_ar": { + "name": "Argentinean Spanish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "et": { + "name": "Estonian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "eu": { + "name": "Basque", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "fa": { + "name": "Persian", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "fi": { + "name": "Finnish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "fil": { + "name": "Filipino", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "fo": { + "name": "Faroese", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "fr": { + "name": "French", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "fur": { + "name": "Friulian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "fy": { + "name": "Frisian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ga": { + "name": "Irish", + "numbers": [ + 1, + 2, + 3, + 7, + 11 + ], + "plurals": function(n) { return Number(n==1 ? 0 : n==2 ? 1 : n<7 ? 2 : n<11 ? 3 : 4) ;} + }, + "gd": { + "name": "Scottish Gaelic", + "numbers": [ + 1, + 2, + 3, + 20 + ], + "plurals": function(n) { return Number((n==1 || n==11) ? 0 : (n==2 || n==12) ? 1 : (n > 2 && n < 20) ? 2 : 3); } + }, + "gl": { + "name": "Galician", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "gu": { + "name": "Gujarati", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "gun": { + "name": "Gun", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "ha": { + "name": "Hausa", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "he": { + "name": "Hebrew", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "hi": { + "name": "Hindi", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "hr": { + "name": "Croatian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "hu": { + "name": "Hungarian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "hy": { + "name": "Armenian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ia": { + "name": "Interlingua", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "id": { + "name": "Indonesian", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "is": { + "name": "Icelandic", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n%10!=1 || n%100==11); } + }, + "it": { + "name": "Italian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ja": { + "name": "Japanese", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "jbo": { + "name": "Lojban", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "jv": { + "name": "Javanese", + "numbers": [ + 0, + 1 + ], + "plurals": function(n) { return Number(n !== 0); } + }, + "ka": { + "name": "Georgian", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "kk": { + "name": "Kazakh", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "km": { + "name": "Khmer", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "kn": { + "name": "Kannada", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ko": { + "name": "Korean", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "ku": { + "name": "Kurdish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "kw": { + "name": "Cornish", + "numbers": [ + 1, + 2, + 3, + 4 + ], + "plurals": function(n) { return Number((n==1) ? 0 : (n==2) ? 1 : (n == 3) ? 2 : 3); } + }, + "ky": { + "name": "Kyrgyz", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "lb": { + "name": "Letzeburgesch", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ln": { + "name": "Lingala", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "lo": { + "name": "Lao", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "lt": { + "name": "Lithuanian", + "numbers": [ + 1, + 2, + 10 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "lv": { + "name": "Latvian", + "numbers": [ + 0, + 1, + 2 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n !== 0 ? 1 : 2); } + }, + "mai": { + "name": "Maithili", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "mfe": { + "name": "Mauritian Creole", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "mg": { + "name": "Malagasy", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "mi": { + "name": "Maori", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "mk": { + "name": "Macedonian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n==1 || n%10==1 ? 0 : 1); } + }, + "ml": { + "name": "Malayalam", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "mn": { + "name": "Mongolian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "mnk": { + "name": "Mandinka", + "numbers": [ + 0, + 1, + 2 + ], + "plurals": function(n) { return Number(0 ? 0 : n==1 ? 1 : 2); } + }, + "mr": { + "name": "Marathi", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ms": { + "name": "Malay", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "mt": { + "name": "Maltese", + "numbers": [ + 1, + 2, + 11, + 20 + ], + "plurals": function(n) { return Number(n==1 ? 0 : n===0 || ( n%100>1 && n%100<11) ? 1 : (n%100>10 && n%100<20 ) ? 2 : 3); } + }, + "nah": { + "name": "Nahuatl", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "nap": { + "name": "Neapolitan", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "nb": { + "name": "Norwegian Bokmal", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ne": { + "name": "Nepali", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "nl": { + "name": "Dutch", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "nn": { + "name": "Norwegian Nynorsk", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "no": { + "name": "Norwegian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "nso": { + "name": "Northern Sotho", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "oc": { + "name": "Occitan", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "or": { + "name": "Oriya", + "numbers": [ + 2, + 1 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "pa": { + "name": "Punjabi", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "pap": { + "name": "Papiamento", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "pl": { + "name": "Polish", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "pms": { + "name": "Piemontese", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ps": { + "name": "Pashto", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "pt": { + "name": "Portuguese", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "pt_br": { + "name": "Brazilian Portuguese", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "rm": { + "name": "Romansh", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ro": { + "name": "Romanian", + "numbers": [ + 1, + 2, + 20 + ], + "plurals": function(n) { return Number(n==1 ? 0 : (n===0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2); } + }, + "ru": { + "name": "Russian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "sah": { + "name": "Yakut", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "sco": { + "name": "Scots", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "se": { + "name": "Northern Sami", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "si": { + "name": "Sinhala", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "sk": { + "name": "Slovak", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number((n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2); } + }, + "sl": { + "name": "Slovenian", + "numbers": [ + 5, + 1, + 2, + 3 + ], + "plurals": function(n) { return Number(n%100==1 ? 1 : n%100==2 ? 2 : n%100==3 || n%100==4 ? 3 : 0); } + }, + "so": { + "name": "Somali", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "son": { + "name": "Songhay", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "sq": { + "name": "Albanian", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "sr": { + "name": "Serbian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "su": { + "name": "Sundanese", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "sv": { + "name": "Swedish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "sw": { + "name": "Swahili", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "ta": { + "name": "Tamil", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "te": { + "name": "Telugu", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "tg": { + "name": "Tajik", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "th": { + "name": "Thai", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "ti": { + "name": "Tigrinya", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "tk": { + "name": "Turkmen", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "tr": { + "name": "Turkish", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "tt": { + "name": "Tatar", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "ug": { + "name": "Uyghur", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "uk": { + "name": "Ukrainian", + "numbers": [ + 1, + 2, + 5 + ], + "plurals": function(n) { return Number(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); } + }, + "ur": { + "name": "Urdu", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "uz": { + "name": "Uzbek", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "vi": { + "name": "Vietnamese", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "wa": { + "name": "Walloon", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n > 1); } + }, + "wo": { + "name": "Wolof", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + }, + "yo": { + "name": "Yoruba", + "numbers": [ + 1, + 2 + ], + "plurals": function(n) { return Number(n != 1); } + }, + "zh": { + "name": "Chinese", + "numbers": [ + 1 + ], + "plurals": function(n) { return 0; } + } + }, + + // for demonstration only sl and ar is added but you can add your own pluralExtensions + addRule: function(lng, obj) { + pluralExtensions.rules[lng] = obj; + }, + + setCurrentLng: function(lng) { + if (!pluralExtensions.currentRule || pluralExtensions.currentRule.lng !== lng) { + var parts = lng.split('-'); + + pluralExtensions.currentRule = { + lng: lng, + rule: pluralExtensions.rules[parts[0]] + }; + } + }, + + get: function(lng, count) { + var parts = lng.split('-'); + + function getResult(l, c) { + var ext; + if (pluralExtensions.currentRule && pluralExtensions.currentRule.lng === lng) { + ext = pluralExtensions.currentRule.rule; + } else { + ext = pluralExtensions.rules[l]; + } + if (ext) { + var i = ext.plurals(c); + var number = ext.numbers[i]; + if (ext.numbers.length === 2 && ext.numbers[0] === 1) { + if (number === 2) { + number = -1; // regular plural + } else if (number === 1) { + number = 1; // singular + } + }//console.log(count + '-' + number); + return number; + } else { + return c === 1 ? '1' : '-1'; + } + } + + return getResult(parts[0], count); + } + + }; + var postProcessors = {}; + var addPostProcessor = function(name, fc) { + postProcessors[name] = fc; + }; + // sprintf support + var sprintf = (function() { + function get_type(variable) { + return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase(); + } + function str_repeat(input, multiplier) { + for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */} + return output.join(''); + } + + var str_format = function() { + if (!str_format.cache.hasOwnProperty(arguments[0])) { + str_format.cache[arguments[0]] = str_format.parse(arguments[0]); + } + return str_format.format.call(null, str_format.cache[arguments[0]], arguments); + }; + + str_format.format = function(parse_tree, argv) { + var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length; + for (i = 0; i < tree_length; i++) { + node_type = get_type(parse_tree[i]); + if (node_type === 'string') { + output.push(parse_tree[i]); + } + else if (node_type === 'array') { + match = parse_tree[i]; // convenience purposes only + if (match[2]) { // keyword argument + arg = argv[cursor]; + for (k = 0; k < match[2].length; k++) { + if (!arg.hasOwnProperty(match[2][k])) { + throw(sprintf('[sprintf] property "%s" does not exist', match[2][k])); + } + arg = arg[match[2][k]]; + } + } + else if (match[1]) { // positional argument (explicit) + arg = argv[match[1]]; + } + else { // positional argument (implicit) + arg = argv[cursor++]; + } + + if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) { + throw(sprintf('[sprintf] expecting number but found %s', get_type(arg))); + } + switch (match[8]) { + case 'b': arg = arg.toString(2); break; + case 'c': arg = String.fromCharCode(arg); break; + case 'd': arg = parseInt(arg, 10); break; + case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break; + case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break; + case 'o': arg = arg.toString(8); break; + case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break; + case 'u': arg = Math.abs(arg); break; + case 'x': arg = arg.toString(16); break; + case 'X': arg = arg.toString(16).toUpperCase(); break; + } + arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg); + pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' '; + pad_length = match[6] - String(arg).length; + pad = match[6] ? str_repeat(pad_character, pad_length) : ''; + output.push(match[5] ? arg + pad : pad + arg); + } + } + return output.join(''); + }; + + str_format.cache = {}; + + str_format.parse = function(fmt) { + var _fmt = fmt, match = [], parse_tree = [], arg_names = 0; + while (_fmt) { + if ((match = /^[^\x25]+/.exec(_fmt)) !== null) { + parse_tree.push(match[0]); + } + else if ((match = /^\x25{2}/.exec(_fmt)) !== null) { + parse_tree.push('%'); + } + else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) { + if (match[2]) { + arg_names |= 1; + var field_list = [], replacement_field = match[2], field_match = []; + if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { + if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + } + else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + } + else { + throw('[sprintf] huh?'); + } + } + } + else { + throw('[sprintf] huh?'); + } + match[2] = field_list; + } + else { + arg_names |= 2; + } + if (arg_names === 3) { + throw('[sprintf] mixing positional and named placeholders is not (yet) supported'); + } + parse_tree.push(match); + } + else { + throw('[sprintf] huh?'); + } + _fmt = _fmt.substring(match[0].length); + } + return parse_tree; + }; + + return str_format; + })(); + + var vsprintf = function(fmt, argv) { + argv.unshift(fmt); + return sprintf.apply(null, argv); + }; + + addPostProcessor("sprintf", function(val, key, opts) { + if (!opts.sprintf) return val; + + if (Object.prototype.toString.apply(opts.sprintf) === '[object Array]') { + return vsprintf(val, opts.sprintf); + } else if (typeof opts.sprintf === 'object') { + return sprintf(val, opts.sprintf); + } + + return val; + }); + // public api interface + i18n.init = init; + i18n.setLng = setLng; + i18n.preload = preload; + i18n.addResourceBundle = addResourceBundle; + i18n.loadNamespace = loadNamespace; + i18n.loadNamespaces = loadNamespaces; + i18n.setDefaultNamespace = setDefaultNamespace; + i18n.t = translate; + i18n.translate = translate; + i18n.exists = exists; + i18n.detectLanguage = f.detectLanguage; + i18n.pluralExtensions = pluralExtensions; + i18n.sync = sync; + i18n.functions = f; + i18n.lng = lng; + i18n.addPostProcessor = addPostProcessor; + i18n.options = o; + + $.i18n = i18n; + $.t = i18n.t; + + return i18n; + +})); \ No newline at end of file diff --git a/nailgun/static/js/main.js b/nailgun/static/js/main.js index 73fc26b9c3..485198849d 100644 --- a/nailgun/static/js/main.js +++ b/nailgun/static/js/main.js @@ -23,6 +23,7 @@ requirejs.config({ 'jquery-timeout': 'js/libs/jquery.timeout', 'jquery-ui': 'js/libs/jquery-ui-1.10.2.custom', 'jquery-autoNumeric': 'js/libs/autoNumeric', + i18next: 'js/libs/i18next.amd.withJQuery-1.6.3', utils: 'js/utils', underscore: 'js/libs/lodash', backbone: 'js/libs/backbone', @@ -57,6 +58,9 @@ requirejs.config({ bootstrap: { deps: ['jquery'] }, + i18next: { + deps: ['jquery'] + }, 'jquery-checkbox': { deps: ['jquery'] }, @@ -70,12 +74,16 @@ requirejs.config({ deps: ['jquery'] }, app: { - deps: ['jquery', 'underscore', 'backbone', 'stickit', 'deepModel', 'coccyx', 'bootstrap', 'retina', 'jquery-checkbox', 'jquery-timeout', 'jquery-ui', 'jquery-autoNumeric'] + deps: ['jquery', 'underscore', 'backbone', 'stickit', 'deepModel', 'coccyx', 'i18next', 'bootstrap', 'retina', 'jquery-checkbox', 'jquery-timeout', 'jquery-ui', 'jquery-autoNumeric'] } } }); -require(['app'], function (app) { +require(['app', 'i18next', 'text!i18n/translation.json'], function (app, i18n, translation) { 'use strict'; - $(document).ready(app.initialize); + $.i18n.init({ + resStore: JSON.parse(translation) + }).always(function() { + $(document).ready(app.initialize); + }); }); diff --git a/nailgun/static/js/views/clusters_page.js b/nailgun/static/js/views/clusters_page.js index 30553e6117..f1d257780a 100644 --- a/nailgun/static/js/views/clusters_page.js +++ b/nailgun/static/js/views/clusters_page.js @@ -37,6 +37,7 @@ function(models, utils, commonViews, dialogViews, clustersPageTemplate, clusterT var clustersView = new ClusterList({collection: this.collection}); this.registerSubView(clustersView); this.$('.cluster-list').html(clustersView.render().el); + this.$el.i18n(); return this; } }); diff --git a/nailgun/static/js/views/common.js b/nailgun/static/js/views/common.js index cad87e8d11..fade23fe2c 100644 --- a/nailgun/static/js/views/common.js +++ b/nailgun/static/js/views/common.js @@ -245,6 +245,21 @@ function(utils, models, dialogViews, navbarTemplate, nodesStatsTemplate, notific views.Footer = Backbone.View.extend({ template: _.template(footerTemplate), + events: { + 'click .footer-lang li a': 'setLocale' + }, + locales: [ + {name: 'EN', locale: 'en-US'}, + {name: 'CN', locale: 'zh-CN'} + ], + setLocale: function(e) { + var newLocale = _.find(this.locales, {locale: $(e.currentTarget).data('locale')}); + $.i18n.setLng(newLocale.locale); + window.location.reload(); + }, + getCurrentLocale: function() { + return _.find(this.locales, {locale: $.i18n.lng()}) || this.locales[0]; + }, initialize: function(options) { $.ajax({url: '/api/version'}).done(_.bind(function(data) { this.version = data.release; @@ -252,7 +267,11 @@ function(utils, models, dialogViews, navbarTemplate, nodesStatsTemplate, notific }, this)); }, render: function() { - this.$el.html(this.template({version: this.version})); + this.$el.html(this.template({ + version: this.version, + locales: this.locales, + currentLocale: this.getCurrentLocale() + })); return this; } }); diff --git a/nailgun/static/js/views/dialogs.js b/nailgun/static/js/views/dialogs.js index 41a6fd9510..68c201ec37 100644 --- a/nailgun/static/js/views/dialogs.js +++ b/nailgun/static/js/views/dialogs.js @@ -89,7 +89,7 @@ function(require, utils, models, simpleMessageTemplate, createClusterWizardTempl var commonViews = require('views/common'); // avoid circular dependencies this.rhelCredentialsForm = new commonViews.RhelCredentialsForm(_.extend({dialog: this}, options)); this.registerSubView(this.rhelCredentialsForm); - this.$('.credentials').html('').append(this.rhelCredentialsForm.render().el); + this.$('.credentials').html('').append(this.rhelCredentialsForm.render().el).i18n(); } }; @@ -527,6 +527,7 @@ function(require, utils, models, simpleMessageTemplate, createClusterWizardTempl this.tearDownRegisteredSubViews(); this.constructor.__super__.render.call(this); this.renderRhelCredentialsForm({redHatAccount: this.redHatAccount}); + this.$el.i18n(); return this; } }, rhelCredentialsMixin)); diff --git a/nailgun/static/js/views/releases_page.js b/nailgun/static/js/views/releases_page.js index 3378a5ee41..8bfd1a9c69 100644 --- a/nailgun/static/js/views/releases_page.js +++ b/nailgun/static/js/views/releases_page.js @@ -55,6 +55,7 @@ function(utils, commonViews, dialogViews, releasesListTemplate, releaseTemplate) this.registerSubView(releaseView); this.$('.releases-table tbody').append(releaseView.render().el); }, this); + this.$el.i18n(); return this; } }); diff --git a/nailgun/static/js/views/support_page.js b/nailgun/static/js/views/support_page.js index b69a00aa26..d1f8068449 100644 --- a/nailgun/static/js/views/support_page.js +++ b/nailgun/static/js/views/support_page.js @@ -91,7 +91,7 @@ function(commonViews, models, supportPageTemplate) { } }, render: function() { - this.$el.html(this.template({tasks: this.logsPackageTasks})); + this.$el.html(this.template({tasks: this.logsPackageTasks})).i18n(); this.stickit(this.fuelKey); return this; } diff --git a/nailgun/static/templates/clusters/cluster.html b/nailgun/static/templates/clusters/cluster.html index 086cc1d005..81f3412672 100755 --- a/nailgun/static/templates/clusters/cluster.html +++ b/nailgun/static/templates/clusters/cluster.html @@ -3,7 +3,7 @@
<% if (!nodes.deferred || nodes.deferred.state() == 'resolved') { %>
-
Nodes:
+
Nodes:
<%= nodes.length %>
<% if (nodes.length) { %>
CPU (cores):
@@ -24,7 +24,7 @@
<% } else { %> - <% var statuses = {'new': 'New', 'deployment': 'Deploying', 'operational': 'Operational', 'error': 'Error', 'remove': 'Removing'} %> + <% var statuses = {'new': $.t('clusters_page.stat_new', {defaultValue: 'New'}), 'deployment': $.t('clusters_page.stat_deploying', {defaultValue: 'Deploying'}), 'operational': $.t('clusters_page.stat_operational', {defaultValue: 'Operational'}), 'error': $.t('clusters_page.stat_error', {defaultValue: 'Error'}), 'remove': $.t('clusters_page.stat_removing', {defaultValue: 'Removing'}) } %> <%= statuses[cluster.get('status')] %> <% } %>
diff --git a/nailgun/static/templates/clusters/new.html b/nailgun/static/templates/clusters/new.html index f7f91232ae..7b95d55a16 100755 --- a/nailgun/static/templates/clusters/new.html +++ b/nailgun/static/templates/clusters/new.html @@ -1,4 +1,4 @@
-
New OpenStack Environment
+
New OpenStack Environment
diff --git a/nailgun/static/templates/clusters/page.html b/nailgun/static/templates/clusters/page.html index c410b8e461..29a5b38e6b 100755 --- a/nailgun/static/templates/clusters/page.html +++ b/nailgun/static/templates/clusters/page.html @@ -1,2 +1,2 @@ -

My OpenStack Environments

+

My OpenStack Environments

diff --git a/nailgun/static/templates/common/breadcrumb.html b/nailgun/static/templates/common/breadcrumb.html index 9b682cb925..b13bbb5f08 100644 --- a/nailgun/static/templates/common/breadcrumb.html +++ b/nailgun/static/templates/common/breadcrumb.html @@ -2,10 +2,10 @@ <% _.each(path, function(part) { %> <% if (_.isArray(part)) { %>
  • - <%- part[0] %> / + <%- $.t('breadcrumb.'+part[0], {defaultValue: part[0]}) %> /
  • <% } else { %> -
  • <%- part %>
  • +
  • <%- $.t('breadcrumb.'+part, {defaultValue: part}) %>
  • <% } %> <% }) %> diff --git a/nailgun/static/templates/common/footer.html b/nailgun/static/templates/common/footer.html index 9a5a1e5723..39a7025e85 100644 --- a/nailgun/static/templates/common/footer.html +++ b/nailgun/static/templates/common/footer.html @@ -4,4 +4,16 @@ <% if (version) { %> <% } %> + diff --git a/nailgun/static/templates/common/navbar.html b/nailgun/static/templates/common/navbar.html index b0c1ca9e81..c685c4d877 100755 --- a/nailgun/static/templates/common/navbar.html +++ b/nailgun/static/templates/common/navbar.html @@ -5,7 +5,7 @@ <% _.each(elements, function(element) { %> -
  • <%- element[0] %>
  • +
  • <%- $.t('navbar.' + element[0], {defaultValue: element[0]}) %>
  • <% }) %>
  • diff --git a/nailgun/static/templates/common/rhel_credentials.html b/nailgun/static/templates/common/rhel_credentials.html index b697abb232..609cf74346 100644 --- a/nailgun/static/templates/common/rhel_credentials.html +++ b/nailgun/static/templates/common/rhel_credentials.html @@ -28,28 +28,28 @@
    - +
    - +
    - +
    - +
    diff --git a/nailgun/static/templates/dialogs/rhel_license.html b/nailgun/static/templates/dialogs/rhel_license.html index 673c353e58..c205c37ea9 100755 --- a/nailgun/static/templates/dialogs/rhel_license.html +++ b/nailgun/static/templates/dialogs/rhel_license.html @@ -1,14 +1,14 @@ diff --git a/nailgun/static/templates/release/release.html b/nailgun/static/templates/release/release.html index 4c5886e967..776f37740d 100755 --- a/nailgun/static/templates/release/release.html +++ b/nailgun/static/templates/release/release.html @@ -12,7 +12,7 @@
    Downloading: 0%
    <% } else { %> - <% var stateMessages = {'available': 'Active', 'error': 'Error', 'not_available': 'Not available', 'downloading': 'Downloading'}; + <% var stateMessages = {'available': $.t('release_page.active', {defaultValue: 'Active'}), 'error': $.t('release_page.error', {defaultValue: 'Error'}), 'not_available': $.t('release_page.not_available', {defaultValue: 'Not available'}), 'downloading': $.t('release_page.downloading', {defaultValue: 'Downloading'})}; var state = stateMessages[release.get('state')]; %> <%= state %> @@ -20,6 +20,6 @@ <% if (release.get('operating_system') == 'RHEL') { %> - + <% } %> diff --git a/nailgun/static/templates/support/page.html b/nailgun/static/templates/support/page.html index 74f597e757..b13857a10b 100755 --- a/nailgun/static/templates/support/page.html +++ b/nailgun/static/templates/support/page.html @@ -1,6 +1,6 @@
    -

    Support

    +

    Support

    @@ -25,13 +25,13 @@

    Contact Support

    -

    +

    Do not hesitate to report a problem or suggest an improvement by clicking the button below. We will get back to you as soon as possible.

    -

    If you have any questions, you can reach out developers at IRC channel #fuel on freenode.net. +

    If you have any questions, you can reach out developers at IRC channel #fuel on freenode.net.

    - Contact support + Contact support

    @@ -42,7 +42,7 @@

    Download Diagnostic Snapshot

    -

    +

    If you have encountered some bugs or errors while using Fuel, you may also need to provide the support team with Fuel logs. You can download them by clicking the button below.