improved docstring and comments in languages.py, thanks Paolo

This commit is contained in:
mdipierro
2018-05-01 11:09:17 -05:00
parent 38482d4846
commit 3170c360aa
+70 -18
View File
@@ -798,7 +798,7 @@ class translator(object):
""" """
Use ## to add a comment into a translation string Use ## to add a comment into a translation string
the comment can be useful do discriminate different possible the comment can be useful do discriminate different possible
translations for the same string (for example different locations):: translations for the same string (for example different locations):
T(' hello world ') -> ' hello world ' T(' hello world ') -> ' hello world '
T(' hello world ## token') -> ' hello world ' T(' hello world ## token') -> ' hello world '
@@ -841,24 +841,71 @@ class translator(object):
""" """
def sub_plural(m): def sub_plural(m):
"""String in `%{}` is transformed by this rules: """String in `%{}` is transformed by this rules:
If string starts with `\\`, `!` or `?` such transformations If string starts with `!` or `?` such transformations
take place:: take place:
"!string of words" -> "String of word" (Capitalize) "!string of words" -> "String of word" (Capitalize)
"!!string of words" -> "String Of Word" (Title) "!!string of words" -> "String Of Word" (Title)
"!!!string of words" -> "STRING OF WORD" (Upper) "!!!string of words" -> "STRING OF WORD" (Upper)
"\\!string of words" -> "!string of word"
(remove \\ and disable transformations)
"?word?number" -> "word" (return word, if number == 1)
"?number" or "??number" -> "" (remove number,
if number == 1)
"?word?number" -> "number" (if number != 1)
"?word1?number" -> "word1" or "number"
(return word1 if number == 1,
return number otherwise)
"??number" or "?number" -> "" or "number"
(as above with word1 = "")
"?word1?number?word0" -> "word1" or "number" or "word0"
(return word1 if number == 1,
return word0 if number == 0,
return number otherwise)
"?word1?number?" -> "word1" or "number" or ""
(as above with word0 = "")
"??number?word0" -> "number" or "word0"
(as above with word1 = "")
"??number?" -> "number" or ""
(as above with word1 = word0 = "")
"?word1?word[number]" -> "word1" or "word"
(return word1 if symbols[number] == 1,
return word otherwise)
"?word1?[number]" -> "" or "word1"
(as above with word = "")
"??word[number]" or "?word[number]" -> "" or "word"
(as above with word1 = "")
"?word1?word?word0[number]" -> "word1" or "word" or "word0"
(return word1 if symbols[number] == 1,
return word0 if symbols[number] == 0,
return word otherwise)
"?word1?word?[number]" -> "word1" or "word" or ""
(as above with word0 = "")
"??word?word0[number]" -> "" or "word" or "word0"
(as above with word1 = "")
"??word?[number]" -> "" or "word"
(as above with word1 = word0 = "")
Other strings, (those not starting with `!` or `?`)
are processed by self.plural
""" """
def sub_tuple(m): def sub_tuple(m):
""" word[number], !word[number], !!word[number], !!!word[number] """ word
word, !word, !!word, !!!word, ?word?number, ??number, ?number !word, !!word, !!!word
?word?word[number], ?word?[number], ??word[number] ?word1?number
??number, ?number
?word1?number?word0
?word1?number?
??number?word0
??number?
word[number]
!word[number], !!word[number], !!!word[number]
?word1?word[number]
?word1?[number]
??word[number], ?word[number]
?word1?word?word0[number]
?word1?word?[number]
??word?word0[number]
??word?[number]
""" """
w, i = m.group('w', 'i') w, i = m.group('w', 'i')
c = w[0] c = w[0]
@@ -876,7 +923,7 @@ class translator(object):
return m.group(0) return m.group(0)
num = int(part2) num = int(part2)
else: else:
# ?[word]?word2[?word3][number] # ?[word1]?word[?word0][number]
num = int(symbols[int(i or 0)]) num = int(symbols[int(i or 0)])
return part1 if num == 1 else part3 if num == 0 else part2 return part1 if num == 1 else part3 if num == 0 else part2
elif w.startswith('!!!'): elif w.startswith('!!!'):
@@ -893,10 +940,15 @@ class translator(object):
return fun(word) return fun(word)
def sub_dict(m): def sub_dict(m):
""" word(var), !word(var), !!word(var), !!!word(var) """ word(key or num)
word(num), !word(num), !!word(num), !!!word(num) !word(key or num), !!word(key or num), !!!word(key or num)
?word2(var), ?word1?word2(var), ?word1?word2?word0(var) ?word1?word(key or num)
?word2(num), ?word1?word2(num), ?word1?word2?word0(num) ??word(key or num), ?word(key or num)
?word1?word?word0(key or num)
?word1?word?(key or num)
??word?word0(key or num)
?word1?word?(key or num)
??word?(key or num), ?word?(key or num)
""" """
w, n = m.group('w', 'n') w, n = m.group('w', 'n')
c = w[0] c = w[0]
@@ -904,7 +956,7 @@ class translator(object):
if c not in '!?': if c not in '!?':
return self.plural(w, n) return self.plural(w, n)
elif c == '?': elif c == '?':
# ?[word1]?word2[?word0](var or num), ?[word1]?word2(var or num) or ?word2(var or num) # ?[word1]?word[?word0](key or num), ?[word1]?word(key or num) or ?word(key or num)
(p1, sep, p2) = w[1:].partition("?") (p1, sep, p2) = w[1:].partition("?")
part1 = p1 if sep else "" part1 = p1 if sep else ""
(part2, sep, part3) = (p2 if sep else p1).partition("?") (part2, sep, part3) = (p2 if sep else p1).partition("?")