define(["connectionManager","userSettings","events"],function(connectionManager,userSettings,events){"use strict";functiongetCurrentLocale(){returncurrentCulture}functiongetDefaultLanguage(){varculture=document.documentElement.getAttribute("data-culture");returnculture?culture:navigator.language?navigator.language:navigator.userLanguage?navigator.userLanguage:navigator.languages&&navigator.languages.length?navigator.languages[0]:"en-us"}functionupdateCurrentCulture(){varculture;try{culture=userSettings.get("language")}catch(err){}culture=culture||getDefaultLanguage(),currentCulture=normalizeLocaleName(culture),ensureTranslations(currentCulture)}functionensureTranslations(culture){for(variinallTranslations)ensureTranslation(allTranslations[i],culture)}functionensureTranslation(translationInfo,culture){returntranslationInfo.dictionaries[culture]?Promise.resolve():loadTranslation(translationInfo.translations,culture).then(function(dictionary){translationInfo.dictionaries[culture]=dictionary})}functionnormalizeLocaleName(culture){culture=culture.replace("_","-");varparts=culture.split("-");2===parts.length&&parts[0].toLowerCase()===parts[1].toLowerCase()&&(culture=parts[0].toLowerCase());varlower=culture.toLowerCase();return"ca-es"===lower?"ca":"sv-se"===lower?"sv":lower}functiongetDictionary(module){module||(module=defaultModule());vartranslations=allTranslations[module];returntranslations?translations.dictionaries[getCurrentLocale()]:{}}functionregister(options){allTranslations[options.name]={translations:options.strings||options.translations,dictionaries:{}}}functionloadStrings(options){varlocale=getCurrentLocale();return"string"==typeofoptions?ensureTranslation(allTranslations[options],locale):(register(options),ensureTranslation(allTranslations[options.name],locale))}functionloadTranslation(translations,lang){lang=normalizeLocaleName(lang);varfiltered=translations.filter(function(t){returnnormalizeLocaleName(t.lang)===lang});returnfiltered.length||(filtered=translations.filter(function(t){return"en-us"===normalizeLocaleName(t.lang)})),newPromise(function(resolve,reject){if(!filtered.length)returnvoidresolve();varurl=filtered[0].path;url+=url.indexOf("?")===-1?"?":"&",url+="v="+cacheParam;varxhr=newXMLHttpRequest;xhr.open("GET",url,!0),xhr.onload=function(e){resolve(this.status<400?JSON.parse(this.response):{})},xhr.onerror=function(){resolve({})},xhr.send()})}functiontranslateKey(key){varmodule,parts=key.split("#");returnparts.length>1&&(module=parts[0],key=parts[1]),translateKeyFromModule(key,module)}functiontranslateKeyFromModule(key,module){vardictionary=getDictionary(module);returndictionary?dictionary[key]||key:key}functionreplaceAll(str,find,replace){returnstr.split(find).join(replace)}functiontranslate(key){for(varval=translateKey(key),i=1;i<arguments.length;i++)val=replaceAll(val,"{"+(i-1)+"}",arguments[i]);returnval}functiontranslateHtml(html,module){if(module||(module=defaultModule()),!module)thrownewError("module cannot be null or empty");varstartIndex=html.indexOf("${");if(startIndex===-1)returnhtml;startIndex+=2;varendIndex=html.indexOf("}",startIndex);if(endIndex===-1)returnhtml;varkey=html.substring(startIndex,endIndex),val=translateKeyFromModule(key,module);returnhtml=html.replace("${"+key+"}",val),translateHtml(html,module)}functiondefaultModule(val){returnval&&(_defaultModule=val),_defaultModule}varcurrentCulture,_defaultModule,allTranslations={},cacheParam=(newDate).getTime();returnupdateCurrentCulture(),events.on(connectionManager,"localusersignedin",updateCurrentCulture),events.on(userSettings,"change",function(e,name){"language"===name&&updateCurrentCulture()}),{getString:translate,translate:translate,translateDocument:translateHtml,translateHtml:translateHtml,loadStrings:loadStrings,defaultModule:defaultModule,getCurrentLocale:getCurrentLocale,register:register}});