define([ "dojo/_base/declare", "dojo/string", "dojo/_base/array", "dojo/json", "jed/jed", "dojo/_base/kernel"], function(declare, string, array, JSON, Jed, kernel) { var module = declare("obno.i18n.translate", null, { }); /** * Loads locale messages from portable object (json version) translation file. Each json stream may contain translations for multiple locales * JSON content consists of one or more translation bundles. Each bundle containing translations for a specific locale. * { * "bundles": [ * { * "locale": "en-us", * "pluralForm": "the plural form for en-us", * "resources": [ * //the translations for this bundle * ] * }, * { * "locale": "el-gr", * "pluralForm": "the plural form greek", * "resources": [ * //the translations for this bundle * ] * } * ] * } */ module.loadBundle = function(poJson) { var poMsgs = JSON.parse(poJson); var bundles = poMsgs["bundles"] || []; array.forEach(bundles, function(bundle){ var jedMsgs = { "domain": "messages", "locale_data": { "messages": { "": { "domain": "messages", "plural_forms": bundle.pluralForm || "nplurals=2; plural=(n != 1);", "lang": (bundle.locale || "").replace('_', '-').toLowerCase() } } } }; array.forEach(bundle.resources, function(msg){ if (msg.msgID.length > 0){ var key = msg.msgID; var pluralKey = msg.msgIDPlural; if (msg.msgCtx){ key = msg.msgCtx + '\u0004' + key; } var translations; if (pluralKey){ translations = msg.plurals || []; } else { translations = [msg.msgStr]; } jedMsgs['locale_data']['messages'][key] = translations; } }); var locale = (bundle.locale || "").replace('_', '-').toLowerCase(); if (module.messages[locale]){ //merge into existing messages var conf = module.messages[locale]["conf"]; var oldMsgs = conf['locale_data']['messages']; var newMsgs = jedMsgs['locale_data']['messages']; for(var key in newMsgs){ if(newMsgs.hasOwnProperty(key) && key.length > 0){ oldMsgs[key] = newMsgs[key]; } } module.messages[locale] = {"conf": conf, "i18n": new Jed(conf)}; } else { module.messages[locale] = {"conf": jedMsgs, "i18n": new Jed(jedMsgs)}; } }); }; module.translate = function(context, singular, plural, n) { var locale = kernel.locale; var translations = module.messages[locale]; if (translations){ return translations.i18n.dcnpgettext(null, context, singular, plural, n); } else { return module.untranslated.dcnpgettext(null, context, singular, plural, n); } }; module.messages = {}; module.untranslated = new Jed({}); return module; });