mirror of https://github.com/micromata/borgbackup-butler.git

Fin Reinhard
22.27.2019 910bf7b1c1e58c1b8dbe2ae7aeb362af5788a932
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {getRestServiceUrl} from './global';
 
let dictionary;
let version;
let language;
 
const clearDictionary = () => {
    window.localStorage.removeItem('dictionary');
}
 
const loadDictionary = (version, language) => {
 
    const localDictionary = window.localStorage.getItem('dictionary');
 
    if (localDictionary) {
        const json = JSON.parse(localDictionary);
 
        if (json.version === version && json.language === language) {
            dictionary = json.dictionary;
            return;
        }
        //console.log("Version=" + version + ", lang="+ language + ", json.version=" + json.version + ", json.language=" + json.language);
    } else {
        //console.log("Version=" + version + ", lang="+ language + ", json=undefined");
    }
    fetchNewDictionary(version, language);
};
 
const fetchNewDictionary = (currentVersion, currentLanguage) => {
    //e.log(new Date().toISOString() + ": version=" + currentVersion + ", lang=" + currentLanguage);
    fetch(getRestServiceUrl('i18n/list'), {
        method: 'GET',
        headers: {
            'Accept': 'application/json'
        }
    })
        .then(response => {
            if (!response.ok) {
                throw new Error(response.statusText);
            }
 
            return response.json();
        })
        .then(json => {
            dictionary = json;
            version = currentVersion;
            language = currentLanguage;
            saveDictionary();
        });
};
 
const saveDictionary = () => window.localStorage.setItem('dictionary', JSON.stringify({
    version, language, dictionary
}));
 
const getTranslation = (key, params) => {
 
    if (!dictionary) {
        return '';
    }
 
    let message = dictionary[key];
 
    if (message && params) {
        params.forEach((param, index) => {
            message = message.replace(`{${index}}`, param);
        });
    }
 
    return message;
};
 
export {clearDictionary, getTranslation, loadDictionary, fetchNewDictionary};