register languages with help of a function. that enables advances functionality with fallback-language and makes it generally more robust.
this commit concludes the refactoring of the layout definition.
This commit is contained in:
parent
d9ae1f3783
commit
bb027261b2
5 changed files with 47 additions and 18 deletions
|
@ -67,4 +67,4 @@ var layout = {
|
|||
"12": {9:[7,8,9,10,11]}
|
||||
}
|
||||
};
|
||||
window._uhr.languages['de'] = layout;
|
||||
window,_uhr.register('de', layout);
|
||||
|
|
|
@ -93,4 +93,4 @@ var layout = {
|
|||
}
|
||||
};
|
||||
// Das Layout bei der Uhr unter dem Code "de_CH" registrieren.
|
||||
window._uhr.languages['de_CH'] = layout;
|
||||
window,_uhr.register('de_CH', layout);
|
||||
|
|
|
@ -68,4 +68,4 @@ var layout = {
|
|||
"12": {10:[1,2,3,4,5,6]}
|
||||
}
|
||||
};
|
||||
window._uhr.languages['de_CH_genau'] = layout;
|
||||
window,_uhr.register('de_CH_genau', layout);
|
||||
|
|
|
@ -88,4 +88,4 @@ var layout = {
|
|||
return hour;
|
||||
}
|
||||
};
|
||||
window._uhr.languages['en'] = layout;
|
||||
window,_uhr.register('en', layout);
|
||||
|
|
57
uhr.js
57
uhr.js
|
@ -13,7 +13,7 @@ You should have received a copy of the GNU General Public License
|
|||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
(function($) {
|
||||
"use strict";
|
||||
'use strict';
|
||||
|
||||
if (window._uhr !== undefined) {
|
||||
return;
|
||||
|
@ -22,7 +22,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
window._uhr = {
|
||||
id: 0,
|
||||
languages: [],
|
||||
themes: []
|
||||
themes: [],
|
||||
register: function(code, language) {
|
||||
for (var i = 0; i < this.languages.length; i++) {
|
||||
if (code == this.languages[i].code) {
|
||||
console.error('Error: Language code ' + code + ' cannot be registered for language "' + language.language + '" because it is already registered for language "' + this.languages[i].language + '"!');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
language.code = code;
|
||||
this.languages.push(language);
|
||||
}
|
||||
};
|
||||
// auto-detect themes
|
||||
var styleSheets = $('head link[rel=stylesheet]');
|
||||
|
@ -148,7 +158,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
}
|
||||
},
|
||||
_language: function() {
|
||||
return window._uhr.languages[this.options.language];
|
||||
for (var i = 0; i < window._uhr.languages.length; i++) {
|
||||
var language = window._uhr.languages[i];
|
||||
if (language.code == this.options.language) {
|
||||
return language;
|
||||
}
|
||||
}
|
||||
// fallback: return empty object
|
||||
return {};
|
||||
},
|
||||
_highlight: function(itemClass) {
|
||||
this.element.find('.item.' + itemClass).addClass('active');
|
||||
|
@ -228,17 +245,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
e.after(toggleSwitch);
|
||||
|
||||
// language chooser
|
||||
var options = [];
|
||||
for (var code in window._uhr.languages) {
|
||||
if (window._uhr.languages.hasOwnProperty(code)) {
|
||||
var language = window._uhr.languages[code];
|
||||
options.push('<option value="' + code + '">' + language.language + '</option>');
|
||||
}
|
||||
}
|
||||
if (options.length > 1) {
|
||||
if (window._uhr.languages.length > 1) {
|
||||
var languageChooser = $('<select id="uhr-languagechooser' + this._id + '"></select>');
|
||||
for (var i = 0; i < options.length; i++) {
|
||||
languageChooser.append(options[i]);
|
||||
for (var i = 0; i < window._uhr.languages.length; i++) {
|
||||
var language = window._uhr.languages[i];
|
||||
languageChooser.append('<option value="' + language.code + '">' + language.language + '</option>');
|
||||
}
|
||||
e.after(languageChooser);
|
||||
}
|
||||
|
@ -282,6 +293,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
if (selectedLanguage == undefined || this.options.force) {
|
||||
selectedLanguage = this.options.language;
|
||||
}
|
||||
var found = false;
|
||||
for (var i = 0; i < window._uhr.languages.length; i++) {
|
||||
var code = window._uhr.languages[i].code;
|
||||
if (selectedLanguage == code) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
var fallback;
|
||||
if (window._uhr.languages.length > 0) {
|
||||
fallback = window._uhr.languages[0].code;
|
||||
} else {
|
||||
fallback = '';
|
||||
}
|
||||
console.warn("Language " + selectedLanguage + " not found! Using fallback: " + fallback);
|
||||
selectedLanguage = fallback;
|
||||
}
|
||||
languageChooser.val(selectedLanguage);
|
||||
this.options.language = "";
|
||||
this.language(selectedLanguage);
|
||||
|
@ -295,7 +324,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
if (selectedTheme == undefined || this.options.force) {
|
||||
selectedTheme = this.options.theme;
|
||||
}
|
||||
var found = false;
|
||||
found = false;
|
||||
for (var i = 0; i < window._uhr.themes.length; i++) {
|
||||
var styleClass = window._uhr.themes[i].styleClass;
|
||||
if (selectedTheme == styleClass) {
|
||||
|
|
Loading…
Reference in a new issue