define(["connectionManager","userSettings","events","apphost"],function(connectionManager,userSettings,events,appHost){"use strict";var currentCulture,currentDateTimeCulture,currentLocales,allTranslations={},allStrings={};function getCurrentLocale(){return currentCulture}function updateCurrentCulture(){var culture,dateTimeCulture;try{culture=userSettings.language()}catch(err){}culture=culture||function(){var culture=document.documentElement.getAttribute("data-culture");return culture||(navigator.language?navigator.language:navigator.userLanguage?navigator.userLanguage:navigator.languages&&navigator.languages.length?navigator.languages[0]:"en-us")}(),currentCulture=normalizeLocaleName(culture),currentLocales=[currentCulture];try{dateTimeCulture=userSettings.dateTimeLocale()}catch(err){}currentDateTimeCulture=dateTimeCulture?normalizeLocaleName(dateTimeCulture):currentCulture,function(culture){for(var i in allTranslations)ensureTranslation(allTranslations[i],culture)}(currentCulture)}function ensureTranslation(translationInfo,culture){return translationInfo.dictionaries[culture]?Promise.resolve():function(translations,lang){lang=normalizeLocaleName(lang);var filtered=translations.filter(function(t){return normalizeLocaleName(t.lang)===lang});filtered.length||(filtered=translations.filter(function(t){return"en-us"===normalizeLocaleName(t.lang)}));return new Promise(function(resolve,reject){var url,xhr;filtered.length?(url=filtered[0].path,url+=-1===url.indexOf("?")?"?":"&",url+="v="+cacheParam,(xhr=new XMLHttpRequest).open("GET",url,!0),xhr.onload=function(e){this.status<400?resolve(JSON.parse(this.response)):resolve()},xhr.onerror=function(){resolve()},xhr.send()):resolve()})}(translationInfo.translations,culture).then(function(dictionary){translationInfo.dictionaries[culture]=!0,dictionary&&(allStrings[culture]?allStrings[culture]=Object.assign(dictionary,allStrings[culture]||{}):allStrings[culture]=dictionary)})}function normalizeLocaleName(culture){var parts=(culture=culture.replace("_","-")).split("-");2===parts.length&&parts[0].toLowerCase()===parts[1].toLowerCase()&&(culture=parts[0].toLowerCase());var lower=culture.toLowerCase();return"ca-es"===lower?"ca":"sv-se"===lower?"sv":lower}function register(options){allTranslations[options.name]={translations:options.strings||options.translations,dictionaries:{}}}var cacheParam=appHost.appVersion();function translateKey(key){var dictionary=allStrings[currentCulture];if(dictionary){var result=dictionary[key];if(result)return result}return key}function translate(key){for(var find,replace,val=translateKey(key),i=1;i<arguments.length;i++)find="{"+(i-1)+"}",replace=arguments[i],val=val.split(find).join(replace);return val}function translateHtml(html){var startIndex=html.indexOf("${");if(-1===startIndex)return html;startIndex+=2;var endIndex=html.indexOf("}",startIndex);if(-1===endIndex)return html;var key=html.substring(startIndex,endIndex),val=translateKey(key);return translateHtml(html=html.replace("${"+key+"}",val))}return updateCurrentCulture(),events.on(connectionManager,"localusersignedin",updateCurrentCulture),events.on(userSettings,"change",function(e,name){"language"!==name&&"datetimelocale"!==name||updateCurrentCulture()}),{getString:translate,translate:translate,translateDocument:translateHtml,translateHtml:translateHtml,loadStrings:function(options){var locale=currentCulture;return"string"==typeof options?ensureTranslation(allTranslations[options],locale):(register(options),ensureTranslation(allTranslations[options.name],locale))},getCurrentLocale:getCurrentLocale,getCurrentDateTimeLocale:function(){return currentDateTimeCulture},getCurrentLocales:function(){return currentLocales},register:register}});