MCPcopy Index your code
hub / github.com/HelloZeroNet/ZeroNet / translateData

Method translateData

src/Translate/Translate.py:101–133  ·  view source on GitHub ↗
(self, data, translate_table=None, mode="js")

Source from the content-addressed store, hash-verified

99 return self[multi].format(value)
100
101 def translateData(self, data, translate_table=None, mode="js"):
102 if not translate_table:
103 translate_table = self
104
105 patterns = []
106 for key, val in list(translate_table.items()):
107 if key.startswith("_("): # Problematic string: only match if called between _(" ") function
108 key = key.replace("_(", "").replace(")", "").replace(", ", '", "')
109 translate_table[key] = "|" + val
110 patterns.append(re.escape(key))
111
112 def replacer(match):
113 target = translate_table[match.group(1)]
114 if mode == "js":
115 if target and target[0] == "|": # Strict string match
116 if match.string[match.start() - 2] == "_": # Only if the match if called between _(" ") function
117 return '"' + target[1:] + '"'
118 else:
119 return '"' + match.group(1) + '"'
120 return '"' + target + '"'
121 else:
122 return match.group(0)[0] + target + match.group(0)[-1]
123
124 if mode == "html":
125 pattern = '[">](' + "|".join(patterns) + ')["<]'
126 else:
127 pattern = '"(' + "|".join(patterns) + ')"'
128 data = re.sub(pattern, replacer, data)
129
130 if mode == "html":
131 data = data.replace("lang={lang}", "lang=%s" % self.lang) # lang get parameter to .js file to avoid cache
132
133 return data
134
135translate = Translate()

Callers 8

testTranslateStrictMethod · 0.95
testTranslateUtf8Method · 0.95
actionUiMediaMethod · 0.80
actionPatchFileMethod · 0.80
actionUiMediaMethod · 0.80
actionUiMediaMethod · 0.80
actionUiMediaMethod · 0.80

Calls 1

itemsMethod · 0.80

Tested by 3

testTranslateStrictMethod · 0.76
testTranslateUtf8Method · 0.76