File : core/uiLib/Translator.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
*/
16
/*
17
Changes:
18
    - v4.0.0:
19
        - created from v3.6.0
20
Doc reviewed 202208
21
 */
22
23
import theHTMLSanitizer from '../htmlSanitizer/HTMLSanitizer.js';
24
25
/* ------------------------------------------------------------------------------------------------------------------------- */
26
/**
27
This class is used to translate the messages in another language
28
See theTranslator for the one and only one instance of this class
29
*/
30
/* ------------------------------------------------------------------------------------------------------------------------- */
31
32
class Translator {
33
34
    /**
35
    A js Map where the translations are stored, ordered by msgid
36
    @type {Map.<String>}
37
    */
38
39
    #translations = new Map ( );
40
41
    /**
42
    constructor
43
    */
44
45
    constructor ( ) {
46
        Object.freeze ( this );
47
    }
48
49
    /**
50
    Load the translations
51
    @param {JsonObject} translations The translations to load
52
    */
53
54
    setTranslations ( translations ) {
55
        translations.forEach (
56
            translation => this.#translations.set (
57
                translation.msgid,
58
                theHTMLSanitizer.sanitizeToJsString ( translation.msgstr )
59
            )
60
        );
61
    }
62
63
    /**
64
    get a message translated
65
    @param {String} msgid The id to identify the message
66
    @param {?Object} params Parameters to include in the message
67
    @return {String} The message corresponding to the id, eventually with params added, or the
68
    id if the corresponding Translation was not found
69
    */
70
71
    getText ( msgid, params ) {
72
        let translation = this.#translations.get ( msgid );
73
        if ( params && translation ) {
74
            Object.getOwnPropertyNames ( params ).forEach (
75
                propertyName => translation = translation.replace ( '{' + propertyName + '}', params [ propertyName ] )
76
            );
77
        }
78
        return translation ? translation : msgid;
79
    }
80
}
81
82
/* ------------------------------------------------------------------------------------------------------------------------- */
83
/**
84
The one and only one instance of Translator class
85
@type {Translator}
86
*/
87
/* ------------------------------------------------------------------------------------------------------------------------- */
88
89
const theTranslator = new Translator ( );
90
91
export default theTranslator;
92
93
/* --- End of file --------------------------------------------------------------------------------------------------------- */
94