| Conditions | 1 |
| Paths | 7 |
| Total Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) { |
||
| 2 | 'use strict'; |
||
| 3 | return function () { |
||
| 4 | var router; |
||
| 5 | |||
| 6 | function languageSelect(el) { |
||
| 7 | var select = document.createElement('select'); |
||
| 8 | select.className = 'language-switch'; |
||
| 9 | select.setAttribute('aria-label', 'Language'); |
||
| 10 | select.addEventListener('change', setSelectLocale); |
||
| 11 | el.appendChild(select); |
||
| 12 | |||
| 13 | // Keep english |
||
| 14 | select.innerHTML = '<option>Language</option>'; |
||
| 15 | for (var i = 0; i < config.supportedLocale.length; i++) { |
||
| 16 | select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>'; |
||
| 17 | } |
||
| 18 | } |
||
| 19 | |||
| 20 | function setSelectLocale(event) { |
||
| 21 | router.fullUrl({ lang: event.target.value }, false, true); |
||
| 22 | } |
||
| 23 | |||
| 24 | function setLocale(lang) { |
||
| 25 | localStorage.setItem('language', getLocale(lang)); |
||
| 26 | location.reload(); |
||
| 27 | } |
||
| 28 | |||
| 29 | function getLocale(input) { |
||
| 30 | var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; |
||
| 31 | var locale = config.supportedLocale[0]; |
||
| 32 | config.supportedLocale.some(function (item) { |
||
| 33 | if (language.indexOf(item) !== -1) { |
||
| 34 | locale = item; |
||
| 35 | return true; |
||
| 36 | } |
||
| 37 | return false; |
||
| 38 | }); |
||
| 39 | return locale; |
||
| 40 | } |
||
| 41 | |||
| 42 | function setTranslation(json) { |
||
| 43 | _.extend(json); |
||
| 44 | |||
| 45 | if (moment.locale(_.locale()) !== _.locale()) { |
||
| 46 | moment.defineLocale(_.locale(), { |
||
| 47 | longDateFormat: { |
||
| 48 | LT: 'HH:mm', |
||
| 49 | LTS: 'HH:mm:ss', |
||
| 50 | L: 'DD.MM.YYYY', |
||
| 51 | LL: 'D. MMMM YYYY', |
||
| 52 | LLL: 'D. MMMM YYYY HH:mm', |
||
| 53 | LLLL: 'dddd, D. MMMM YYYY HH:mm' |
||
| 54 | }, |
||
| 55 | calendar: json.momentjs.calendar, |
||
| 56 | relativeTime: json.momentjs.relativeTime |
||
| 57 | }); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | function init(r) { |
||
| 62 | router = r; |
||
| 63 | /** global: _ */ |
||
| 64 | window._ = new Polyglot({ locale: getLocale(router.getLang()), allowMissing: true }); |
||
| 65 | helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation); |
||
| 66 | document.querySelector('html').setAttribute('lang', _.locale()); |
||
| 67 | } |
||
| 68 | |||
| 69 | return { |
||
| 70 | init: init, |
||
| 71 | getLocale: getLocale, |
||
| 72 | setLocale: setLocale, |
||
| 73 | languageSelect: languageSelect |
||
| 74 | }; |
||
| 75 | }; |
||
| 76 | }); |
||
| 77 |