|
@@ -2,31 +2,46 @@
|
2
|
2
|
|
3
|
3
|
import json
|
4
|
4
|
|
|
5
|
+## Handle string with translations
|
5
|
6
|
class MlString(object):
|
6
|
|
- """ Handle string with translations """
|
7
|
7
|
|
|
8
|
+ ## Instanciate a new string with translation
|
|
9
|
+ #
|
|
10
|
+ # @param translations dict: With key = lang and value the translation
|
8
|
11
|
def __init__(self, translations = dict()):
|
9
|
12
|
self.translations = translations
|
10
|
|
-
|
|
13
|
+
|
|
14
|
+ ## Return a translation
|
|
15
|
+ # @param lang str: The lang
|
|
16
|
+ # @return An empty string if the wanted lang don't exist
|
|
17
|
+ # @warning Returns an empty string if the wanted translation didn't exists
|
|
18
|
+ # @todo Raise an exception if not exists ?
|
11
|
19
|
def get(self, lang):
|
12
|
20
|
if not lang in self.translations:
|
13
|
21
|
return ''
|
14
|
22
|
|
15
|
23
|
return self.translations[lang]
|
16
|
|
-
|
|
24
|
+
|
|
25
|
+ ## Set a translation for this MlString
|
|
26
|
+ # @param lang str: The language
|
|
27
|
+ # @param text str: The translation
|
17
|
28
|
def set(self, lang, text):
|
18
|
29
|
if not text:
|
19
|
30
|
if lang in self.translations:
|
20
|
31
|
del(self.translations[lang])
|
21
|
32
|
else:
|
22
|
33
|
self.translations[lang] = text
|
23
|
|
-
|
|
34
|
+ ## String representation
|
|
35
|
+ # @return A json dump of the MlString::translations dict
|
24
|
36
|
def __str__(self):
|
25
|
37
|
if self.translations:
|
26
|
38
|
return json.dumps(self.translations)
|
27
|
39
|
else:
|
28
|
40
|
return ""
|
29
|
41
|
|
|
42
|
+ ## Test if two MlString instance are equivalent
|
|
43
|
+ # @param other MlString : Another MlString instance
|
|
44
|
+ # @return True or False
|
30
|
45
|
def __eq__(self, other):
|
31
|
46
|
if not isinstance(other, MlString):
|
32
|
47
|
return False
|
|
@@ -38,9 +53,14 @@ class MlString(object):
|
38
|
53
|
return True
|
39
|
54
|
|
40
|
55
|
@staticmethod
|
|
56
|
+ ## Instanciate a MlString from json
|
|
57
|
+ # @param json_string str: Json datas in a string
|
|
58
|
+ # @return A new MlString instance
|
|
59
|
+ # @warning fails silently
|
41
|
60
|
def load(json_string):
|
42
|
61
|
if isinstance(json_string, str) and json_string != '':
|
43
|
62
|
translations = json.loads(json_string)
|
44
|
63
|
else:
|
45
|
64
|
translations = dict()
|
46
|
65
|
return MlString(translations)
|
|
66
|
+
|