Completed
Branch task/languageSwitch (6d5ba7)
by Xaver
01:04
created

language.js ➔ define   A

Complexity

Conditions 1
Paths 7

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 7
dl 0
loc 64
rs 9.3956
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A language.js ➔ ... ➔ 0 61 1

How to fix   Long Method   

Long Method

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:

1
define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) {
2
  'use strict';
3
  return function (config) {
4
    function languageSelect(el) {
5
      var select = document.createElement('select');
6
      select.className = 'language-switch';
7
      select.addEventListener('change', setLocale);
8
      el.appendChild(select);
9
10
      var option = document.createElement('option');
11
      // Keep english
12
      option.text = 'Language';
13
      select.appendChild(option);
14
15
      for (var i = 0; i < config.supportedLocale.length; i++) {
16
        option = document.createElement('option');
17
        option.text = option.value = config.supportedLocale[i];
18
        select.appendChild(option);
19
      }
20
    }
21
22
    function setLocale(event) {
23
      localStorage.setItem('language', getLocale(event.target.value));
24
      location.reload();
25
    }
26
27
    function getLocale(input) {
28
      var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
29
      var locale = config.supportedLocale[0];
30
      config.supportedLocale.some(function (item) {
31
        if (language.indexOf(item) !== -1) {
32
          locale = item;
33
          return true;
34
        }
35
        return false;
36
      });
37
      return locale;
38
    }
39
40
    function setTranslation(json) {
41
      _.extend(json);
42
43
      moment.locale(_.locale(), {
44
        longDateFormat: {
45
          LT: 'HH:mm',
46
          LTS: 'HH:mm:ss',
47
          L: 'DD.MM.YYYY',
48
          LL: 'D. MMMM YYYY',
49
          LLL: 'D. MMMM YYYY HH:mm',
50
          LLLL: 'dddd, D. MMMM YYYY HH:mm'
51
        },
52
        calendar: json.momentjs.calendar,
53
        relativeTime: json.momentjs.relativeTime
54
      });
55
    }
56
57
    window._ = new Polyglot({ locale: getLocale(), allowMissing: true });
58
    helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation);
59
60
    return {
61
      languageSelect: languageSelect
62
    };
63
  };
64
});
65