Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

translate.py 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. """
  24. Contributing:
  25. 1. Add the .csv file
  26. 2. Run import
  27. 3. Then run translate
  28. """
  29. import webnotes
  30. import os
  31. import codecs
  32. import json
  33. import re
  34. from csv import reader
  35. messages = {}
  36. def translate(lang=None):
  37. languages = [lang]
  38. if lang=="all" or lang==None:
  39. languages = get_all_languages()
  40. print "Extracting / updating translatable strings..."
  41. build_message_files()
  42. print "Compiling messages in one file..."
  43. export_messages(lang, '_lang_tmp.csv')
  44. for lang in languages:
  45. if lang != "en":
  46. filename = 'app/translations/'+lang+'.csv'
  47. print "For " + lang + ":"
  48. print "Translating via Google Translate..."
  49. google_translate(lang, '_lang_tmp.csv', filename)
  50. print "Updating language files..."
  51. import_messages(lang, filename)
  52. print "Deleting temp file..."
  53. os.remove('_lang_tmp.csv')
  54. def get_all_languages():
  55. return [f[:-4] for f in os.listdir("app/translations") if f.endswith(".csv")]
  56. def update_translations():
  57. """
  58. compare language file timestamps with last updated timestamps in `.wnf-lang-status`
  59. if timestamps are missing / changed, build new `.json` files in the `lang folders`
  60. """
  61. langstatus = {}
  62. languages = get_all_languages()
  63. message_updated = False
  64. status_file_path = "app/.wnf-lang-status"
  65. if os.path.exists(status_file_path):
  66. with open(status_file_path, "r") as langstatusfile:
  67. langstatus = eval(langstatusfile.read())
  68. for lang in languages:
  69. filename = 'app/translations/'+lang+'.csv'
  70. if langstatus.get(lang, None)!=os.path.getmtime(filename):
  71. print "Setting up lang files for " + lang + "..."
  72. if not message_updated:
  73. print "Extracting / updating translatable strings..."
  74. build_message_files()
  75. message_updated = True
  76. print "Writing translations..."
  77. import_messages(lang, filename)
  78. langstatus[lang] = os.path.getmtime(filename)
  79. with open(status_file_path, "w") as langstatusfile:
  80. langstatus = langstatusfile.write(str(langstatus))
  81. def build_message_files():
  82. """build from doctypes, pages, database and framework"""
  83. if not webnotes.conn:
  84. webnotes.connect()
  85. build_for_pages('lib/core')
  86. build_for_pages('app')
  87. build_from_doctype_code('lib/core')
  88. build_from_doctype_code('app')
  89. # doctype
  90. build_from_database()
  91. build_for_framework('lib/webnotes', 'py', with_doctype_names=True)
  92. build_for_framework('lib/public/js/wn', 'js')
  93. build_for_framework('app/public/js', 'js', with_doctype_names=True)
  94. def build_for_pages(path):
  95. """make locale files for framework py and js (all)"""
  96. messages = []
  97. for (basepath, folders, files) in os.walk(path):
  98. if os.path.basename(os.path.dirname(basepath))=="page":
  99. messages_js, messages_py = [], []
  100. for fname in files:
  101. if fname.endswith('.js'):
  102. messages_js += get_message_list(os.path.join(basepath, fname))
  103. if fname.endswith('.py'):
  104. messages_py += get_message_list(os.path.join(basepath, fname))
  105. if messages_js:
  106. write_messages_file(basepath, messages_js, "js")
  107. if messages_py:
  108. write_messages_file(basepath, messages_py, "py")
  109. doctype_path = get_doc_path(m[0], 'Module Def', m[0])
  110. write_messages_file(doctype_path, messages, 'doc')
  111. def build_from_database():
  112. """make doctype labels, names, options, descriptions"""
  113. def get_select_options(doc):
  114. if doc.doctype=="DocField" and doc.fieldtype=='Select' and doc.options \
  115. and not doc.options.startswith("link:") \
  116. and not doc.options.startswith("attach_files:"):
  117. return doc.options.split('\n')
  118. else:
  119. return []
  120. build_for_doc_from_database(webnotes._dict({
  121. "doctype": "DocType",
  122. "module_field": "module",
  123. "DocType": ["name", "description", "module"],
  124. "DocField": ["label", "description"],
  125. "custom": get_select_options
  126. }))
  127. def build_for_doc_from_database(fields):
  128. from webnotes.modules import get_doc_path
  129. for item in webnotes.conn.sql("""select name from `tab%s`""" % fields.doctype, as_dict=1):
  130. messages = []
  131. doclist = webnotes.bean(fields.doctype, item.name).doclist
  132. for doc in doclist:
  133. if doc.doctype in fields:
  134. messages += map(lambda x: x in fields[doc.doctype] and doc.fields.get(x) or None,
  135. doc.fields.keys())
  136. if fields.custom:
  137. messages += fields.custom(doc)
  138. doc = doclist[0]
  139. if doc.fields.get(fields.module_field):
  140. doctype_path = get_doc_path(doc.fields[fields.module_field],
  141. doc.doctype, doc.name)
  142. write_messages_file(doctype_path, messages, 'doc')
  143. def build_for_framework(path, mtype, with_doctype_names = False):
  144. """make locale files for framework py and js (all)"""
  145. messages = []
  146. for (basepath, folders, files) in os.walk(path):
  147. for fname in files:
  148. if fname.endswith('.' + mtype):
  149. messages += get_message_list(os.path.join(basepath, fname))
  150. # append module & doctype names
  151. if with_doctype_names:
  152. for m in webnotes.conn.sql("""select name, module from `tabDocType`"""):
  153. messages.append(m[0])
  154. messages.append(m[1])
  155. # append labels from config.json
  156. config = webnotes.get_config()
  157. for m in config["modules"]:
  158. if m.get("label"):
  159. messages.append(m["label"])
  160. if messages:
  161. write_messages_file(path, messages, mtype)
  162. def build_from_doctype_code(path):
  163. """walk and make locale files in all folders"""
  164. for (basepath, folders, files) in os.walk(path):
  165. messagespy = []
  166. messagesjs = []
  167. for fname in files:
  168. if fname.endswith('py'):
  169. messagespy += get_message_list(os.path.join(basepath, fname))
  170. if fname.endswith('js'):
  171. messagesjs += get_message_list(os.path.join(basepath, fname))
  172. if messagespy:
  173. write_messages_file(basepath, messagespy, 'py')
  174. if messagespy:
  175. write_messages_file(basepath, messagesjs, 'js')
  176. def get_message_list(path):
  177. """get list of messages from a code file"""
  178. import re
  179. messages = []
  180. with open(path, 'r') as sourcefile:
  181. txt = sourcefile.read()
  182. messages += re.findall('_\("([^"]*)"\)', txt)
  183. messages += re.findall("_\('([^']*)'\)", txt)
  184. messages += re.findall('_\("{3}([^"]*)"{3}\)', txt, re.S)
  185. return messages
  186. def write_messages_file(path, messages, mtype):
  187. """write messages to translation file"""
  188. if not os.path.exists(path):
  189. return
  190. if not os.path.exists(os.path.join(path, 'locale')):
  191. os.makedirs(os.path.join(path, 'locale'))
  192. fname = os.path.join(path, 'locale', '_messages_' + mtype + '.json')
  193. messages = list(set(messages))
  194. filtered = []
  195. for m in messages:
  196. if m and re.search('[a-zA-Z]+', m):
  197. filtered.append(m)
  198. with open(fname, 'w') as msgfile:
  199. msgfile.write(json.dumps(filtered, indent=1))
  200. def export_messages(lang, outfile):
  201. """get list of all messages"""
  202. messages = {}
  203. # extract messages
  204. for (basepath, folders, files) in os.walk('.'):
  205. def _get_messages(messages, basepath, mtype):
  206. mlist = get_messages(basepath, mtype)
  207. if not mlist:
  208. return
  209. # update messages with already existing translations
  210. langdata = get_lang_data(basepath, lang, mtype)
  211. for m in mlist:
  212. if not messages.get(m):
  213. messages[m] = langdata.get(m, "")
  214. if os.path.basename(basepath)=='locale':
  215. _get_messages(messages, basepath, 'doc')
  216. _get_messages(messages, basepath, 'py')
  217. _get_messages(messages, basepath, 'js')
  218. # remove duplicates
  219. if outfile:
  220. from csv import writer
  221. with open(outfile, 'w') as msgfile:
  222. w = writer(msgfile)
  223. keys = messages.keys()
  224. keys.sort()
  225. for m in keys:
  226. w.writerow([m.encode('utf-8'), messages.get(m, '').encode('utf-8')])
  227. def import_messages(lang, infile):
  228. """make individual message files for each language"""
  229. data = dict(get_all_messages_from_file(infile))
  230. for (basepath, folders, files) in os.walk('.'):
  231. def _update_lang_file(mtype):
  232. """create a langauge file for the given message type"""
  233. messages = get_messages(basepath, mtype)
  234. if not messages: return
  235. # read existing
  236. langdata = get_lang_data(basepath, lang, mtype)
  237. # update fresh
  238. for m in messages:
  239. if data.get(m):
  240. langdata[m] = data.get(m)
  241. if langdata:
  242. # write new langfile
  243. langfilename = os.path.join(basepath, lang + '-' + mtype + '.json')
  244. with open(langfilename, 'w') as langfile:
  245. langfile.write(json.dumps(langdata, indent=1, sort_keys=True).encode('utf-8'))
  246. #print 'wrote ' + langfilename
  247. if os.path.basename(basepath)=='locale':
  248. # make / update lang files for each type of message file (doc, js, py)
  249. # example: hi-doc.json, hi-js.json, hi-py.json
  250. _update_lang_file('doc')
  251. _update_lang_file('js')
  252. _update_lang_file('py')
  253. def get_doc_messages(module, doctype, name):
  254. from webnotes.modules import get_doc_path
  255. return get_lang_data(get_doc_path(module, doctype, name), None, 'doc')
  256. def get_lang_data(basepath, lang, mtype):
  257. """get language dict from langfile"""
  258. # add "locale" folder if reqd
  259. if os.path.basename(basepath) != 'locale':
  260. basepath = os.path.join(basepath, 'locale')
  261. if not lang: lang = webnotes.lang
  262. path = os.path.join(basepath, lang + '-' + mtype + '.json')
  263. langdata = {}
  264. if os.path.exists(path):
  265. with codecs.open(path, 'r', 'utf-8') as langfile:
  266. langdata = json.loads(langfile.read())
  267. return langdata
  268. def get_messages(basepath, mtype):
  269. """load list of messages from _message files"""
  270. # get message list
  271. path = os.path.join(basepath, '_messages_' + mtype + '.json')
  272. messages = []
  273. if os.path.exists(path):
  274. with open(path, 'r') as msgfile:
  275. messages = json.loads(msgfile.read())
  276. return messages
  277. def update_lang_js(jscode, path):
  278. return jscode + "\n\n$.extend(wn._messages, %s)" % \
  279. json.dumps(get_lang_data(path, webnotes.lang, 'js'))
  280. def get_all_messages_from_file(path):
  281. with codecs.open(path, 'r', 'utf-8') as msgfile:
  282. data = msgfile.read()
  283. data = reader([r.encode('utf-8') for r in data.splitlines()])
  284. newdata = []
  285. for row in data:
  286. newrow = []
  287. for val in row:
  288. newrow.append(unicode(val, 'utf-8'))
  289. newdata.append(newrow)
  290. return newdata
  291. def google_translate(lang, infile, outfile):
  292. """translate objects using Google API. Add you own API key for translation"""
  293. data = get_all_messages_from_file(infile)
  294. import requests, conf
  295. # update existing translations
  296. if os.path.exists(outfile):
  297. with codecs.open(outfile, "r", "utf-8") as oldfile:
  298. old_data = oldfile.read()
  299. old_translations = dict(reader([r.encode('utf-8').strip() for r in old_data.splitlines()]))
  300. with open(outfile, 'w') as msgfile:
  301. from csv import writer
  302. w = writer(msgfile)
  303. for row in data:
  304. if row[0] and row[0].strip():
  305. if old_translations.get(row[0].strip()):
  306. row[1] = old_translations[row[0].strip()]
  307. else:
  308. print 'translating: ' + row[0]
  309. response = requests.get("""https://www.googleapis.com/language/translate/v2""",
  310. params = {
  311. "key": conf.google_api_key,
  312. "source": "en",
  313. "target": lang,
  314. "q": row[0]
  315. })
  316. if "error" in response.json:
  317. print response.json
  318. row[1] = response.json["data"]["translations"][0]["translatedText"]
  319. if not row[1]:
  320. row[1] = row[0] # google unable to translate!
  321. row[1] = row[1].encode('utf-8')
  322. row[0] = row[0].encode('utf-8')
  323. w.writerow(row)