File: components/locale/app.js

Recommend this page to a friend!
  Classes of Melanie Wehowski   µ.Flow   components/locale/app.js   Download  
File: components/locale/app.js
Role: Application script
Content type: text/plain
Description: Application script
Class: µ.Flow
General purpose library of objects
Author: By
Last change: introducing Intents
Date: 8 years ago
Size: 11,318 bytes
 

Contents

Class file image Download
/* @copyright (c) Till Wehowski - All rights reserved @license (Basic/EULA) http://look-up.webfan.de/webdof-license @license (Source Code Re-Usage) http://look-up.webfan.de/bsd-license Copyright (c) 2015, Till Wehowski All rights reserved. @component https://github.com/frdl/-Flow/tree/master/components/locale */ (function(global){ 'use strict'; if('undefined'===typeof frdl) throw new Error('The frdl/µ.Flow library is required to run this widget, get it at http://frdl.webfan.de/'); var _CNAME = 'locale'; var tok = 'flow://components/'+_CNAME; var _i = 0; var getID = function(t){ return 'flow-component-'+_CNAME+'-'+ (_i++).toString() + '-' +Guid.newGuid() + '-'+Sha1.hash(Guid.newGuid() + new Date().toString()+t.toString()); }; /* frdl.addReadyCheck(function(){ return ('undefined' !== typeof navigator.mozL10n && 'function'===typeof 'undefined' !== typeof navigator.mozL10n.translate) ? true : false; }); */ try{ (function(){ 'use strict'; try{ if('function'===typeof frdl.langInitiated)return; } catch(err){ } var _DEF = 'default'; var LanguageOptionSelectors = []; var _loaded=false; var widgetsSelector = '*[data-frdl-component="'+tok+'"] ul[data-frdl-component-id*="'+_CNAME+'"]'; frdl.langDefault=function(lang,trans){ if('undefined'!==typeof lang && 'string'===typeof lang){ if(_DEF!==lang){ if('undefined'===typeof $.WebfanDesktop.o || true !== $.WebfanDesktop.o){ return frdl.Throw('framework', 'DefaultLanguage SHOULD only be changed in Debug Mode!'); } } _DEF = lang; if(true===trans){ frdl.translate(lang); } } return _DEF.toString(); }; frdl.langGetClassSelector = function(){ return widgetsSelector; }; frdl.getLanguageOptionSelectors = function(){ return LanguageOptionSelectors; }; frdl.addLanguageOptionSelector = function(id){ LanguageOptionSelectors.push(id); return this; }; frdl.removeLanguageOptionSelector = function(id){ LanguageOptionSelectors = frdl.filter(LanguageOptionSelectors, function(v,i){ if(v !== id)return true; return false; }, false); return this; }; frdl.isLanguageOptionSelector = function(id){ var found = false; frdl.getLanguageOptionSelectors() .forEach(function(element, ix, array){ if(id===array[ix]){ found=true; return false; } }); return found; }; frdl.getLang = function(){ var l = frdl.langDefault(); if('object' === typeof $.WebfanDesktop && 'undefined' !== typeof $.WebfanDesktop.Registry && 'string' === typeof $.WebfanDesktop.Registry.User.lang){ l = $.WebfanDesktop.Registry.User.lang; }else if('string' === typeof WebfanReadCookie('lang.user.selected')){ l =WebfanReadCookie('lang.user.selected'); }else if('string' === typeof wUser.lang){ l = wUser.lang; }else{ l = BrowserDetect.language; } if('string' !== typeof l || frdl.langDefault() === l || frdl.langDefault() === l || new RegExp(/undefined/).test(l.toString()) ){ if( BrowserDetect.language === 'de'){ l = 'de_DE'; }else if( BrowserDetect.language === 'en'){ l= 'en_EN'; }else if( BrowserDetect.language === 'fr'){ l= 'fr_FR'; } else { l = frdl.langDefault(); } } l = str_replace('-','_',l); if(2 > explode('_', l).length && l !== frdl.langDefault()){ l= l + '_' + l.toUpperCase(); } return l; }; frdl.langstr = function(modul){ var url = frdl.route('API_TRANSLATION_MAIN', null, null, modul); frdl.getScript(url, function(){ },false, false); }; frdl.lang = function(langStrings, allSelectors, registeredSelectors){ allSelectors=false;registeredSelectors=true; var languageStrings = langStrings,l = frdl.getLang(); $(document).ready(function(){ frdl.ready(function(){ if(false!==registeredSelectors){ frdl.getLanguageOptionSelectors() .forEach(function(element, ix, array){ try{ $(array[ix]).locale('loadLangs', languageStrings) ; /* $(array[ix]).locale('translate', l); */ }catch(err){ console.error(err); } }); } if(true===allSelectors){ $(frdl.langGetClassSelector()).locale('loadLangs', languageStrings) ; /* $(frdl.langGetClassSelector()).locale('translate', l); */ } frdl.translate(); }); }); }; frdl.localeUrl = function(m, lang){ if(null === lang || 'undefined' === typeof lang){ lang = frdl.getLang(); } if('function'===typeof m){ return m(lang); }else if('string' === typeof m){ return '/locale/'+m+'/'+frdl.getLang()+'/'+frdl.getLang()+'.properties'; }else{ return '/locale/'+frdl.getLang()+'/'+frdl.getLang()+'.properties'; } }; frdl.translate = function(lang, allSelectors, registeredSelectors){ allSelectors=false;registeredSelectors=true; if(null === lang || 'undefined' === typeof lang){ var l = frdl.getLang(); }else{ var l = lang; } wUser.lang = l; if('function'===typeof $.WebfanDesktop.save){ $.WebfanDesktop.Registry.User.lang = wUser.lang; $.WebfanDesktop.save('user'); } WebfanCreateCookie('lang.user.selected',l,31); var url = frdl.localeUrl(null,l); if(null === document.querySelector('link[href="'+url+'"]')){ $('head').append('<link rel="prefetch"' +' type="application/l10n" lang="'+l+'" href="'+url+'"></link>'); } frdl.ready(function(){ navigator.mozL10n.ctx.ready(function(){ if(false!==registeredSelectors){ frdl.getLanguageOptionSelectors() .forEach(function(element, ix, array){ try{ $(array[ix]).locale('translate', l); }catch(err){ console.error(err); } }); } if(true===allSelectors){ try{ $(frdl.langGetClassSelector()).locale('translate', l); }catch(err){ console.error(err); } } }); /* $(document).ready(function(){ */ try{ /* if('undefined' ===typeof navigator.mozL10n)return false; navigator.mozL10n.language.code = l; navigator.mozL10n.requestLocales(); $(window).trigger('languagechange',{ language:{code: explode('_', languageCode)[0]} }); */ if(frdl.getLang()!==lang){ navigator.mozL10n.language.code = l; frdl.translationl10n(); } /* if(frdl.getLang()!==lang){ frdl.getScript('http://api.webfan.de/api-d/4/js-api/library.js?plugin=l10n', function(){ navigator.mozL10n.language.code = l; frdl.each(document.querySelectorAll('*[data-l10n-id]'), function(i,E){ navigator.mozL10n.translate(E); }); },false,false); } */ /* navigator.mozL10n.ctx.requestLocales(l);*/ }catch(err){ console.error(err); } /* }); */ /* window.dispatchEvent(new Event('languagechange')); */ return false; }); }; frdl.langInitiated = function(){ if(false===_loaded){ var defSel = frdl.langGetClassSelector(); /* if(true!==frdl.isLanguageOptionSelector(defSel)){ frdl.addLanguageOptionSelector(defSel); }*/ frdl.langDefault(_DEF); _loaded=true; } return _loaded; }; }()); /* frdl.ready(function(){ frdl.UI.defer(); angular.module('frdl') .controller(_CNAME + '.MainCtrl', function($scope){ $scope.languageCode = frdl.getLang(); }) ; frdl.UI.load(); frdl.UI.defer(); }); */ }catch(err){ console.error('Error initialising translation tool: '+err); } frdl.each(document.querySelectorAll('*[data-frdl-component$="locale"]'), function(i,el){ if('true'===el.getAttribute('data-frdl-component-loaded-script'))return true; el.setAttribute('data-frdl-component-initiated', 'true'); el.setAttribute('data-frdl-component-loaded-script', 'true'); el.setAttribute('data-role', 'none'); el.setAttribute('data-theme', 'none'); el.style.display='inline'; var ul = frdl.Dom.create('ul'),id =getID(frdl.getLang()); ul.style.display='inline-block'; ul.style.width='16px'; ul.style.height='16px'; ul.setAttribute('data-frdl-component-id', id); ul.setAttribute('class', 'localizationTool'); frdl.Dom.add(ul, el); if(null === document.querySelector('link[href="'+frdl.localeUrl(null,frdl.getLang())+'"]')){ $('head').append('<link rel="prefetch"' +' type="application/l10n" lang="'+frdl.getLang()+'" href="'+frdl.localeUrl(null,frdl.getLang())+'"></link>'); } /* var sel = ' ul[data-frdl-component-id="'+id+'"]'; */ var sel = ul; var _def = frdl.langDefault().toString(); var LangTool = $(sel).locale({ languages: { 'default' : { language: _def, country: '@ Webfan', languageTranslated: _def, countryTranslated: 'Webfan', flag: { 'class' : 'flag' } } }, 'defaultLanguage' : _def, 'ignoreUnmatchedSelectors' : true, 'showFlag': true, 'showCountry': true, 'showLanguage': true, 'onLanguageSelected' : function (languageCode) { try{ frdl.translate(languageCode, false, true); }catch(err){ console.error(err); } return true; }, 'strings' : { 'AGB' : { 'en_GB' : 'TOS', 'de_DE' : 'AGB', 'fr_FR' : 'TOS' } } }).css('zIndex', 1009); frdl.addLanguageOptionSelector(LangTool); frdl.Dom.bringToFront($(el)); frdl.Dom.bringToFront($(sel)); frdl.translate(); }); }(typeof exports !== 'undefined' ? global : (typeof this !== 'undefined' ? this : window)));