You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translate.py 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. from webnotes.modules import get_doc_path
  36. messages = {}
  37. def translate(lang=None):
  38. languages = [lang]
  39. if lang=="all" or lang==None:
  40. languages = get_all_languages()
  41. print "Extracting / updating translatable strings..."
  42. build_message_files()
  43. print "Compiling messages in one file..."
  44. export_messages(lang, '_lang_tmp.csv')
  45. for lang in languages:
  46. if lang != "en":
  47. filename = 'app/translations/'+lang+'.csv'
  48. print "For " + lang + ":"
  49. print "Translating via Google Translate..."
  50. google_translate(lang, '_lang_tmp.csv', filename)
  51. print "Updating language files..."
  52. import_messages(lang, filename)
  53. print "Deleting temp file..."
  54. os.remove('_lang_tmp.csv')
  55. def get_all_languages():
  56. return [f[:-4] for f in os.listdir("app/translations") if f.endswith(".csv")]
  57. def update_translations():
  58. """
  59. compare language file timestamps with last updated timestamps in `.wnf-lang-status`
  60. if timestamps are missing / changed, build new `.json` files in the `lang folders`
  61. """
  62. langstatus = {}
  63. languages = get_all_languages()
  64. message_updated = False
  65. status_file_path = "app/.wnf-lang-status"
  66. if os.path.exists(status_file_path):
  67. with open(status_file_path, "r") as langstatusfile:
  68. langstatus = eval(langstatusfile.read())
  69. for lang in languages:
  70. filename = 'app/translations/'+lang+'.csv'
  71. if langstatus.get(lang, None)!=os.path.getmtime(filename):
  72. print "Setting up lang files for " + lang + "..."
  73. if not message_updated:
  74. print "Extracting / updating translatable strings..."
  75. build_message_files()
  76. message_updated = True
  77. print "Writing translations..."
  78. import_messages(lang, filename)
  79. langstatus[lang] = os.path.getmtime(filename)
  80. with open(status_file_path, "w") as langstatusfile:
  81. langstatus = langstatusfile.write(str(langstatus))
  82. def build_message_files():
  83. """build from doctypes, pages, database and framework"""
  84. if not webnotes.conn:
  85. webnotes.connect()
  86. build_for_pages('lib/core')
  87. build_for_pages('app')
  88. build_from_doctype_code('lib/core')
  89. build_from_doctype_code('app')
  90. # doctype
  91. build_from_database()
  92. build_for_framework('lib/webnotes', 'py', with_doctype_names=True)
  93. build_for_framework('lib/public/js/wn', 'js')
  94. build_for_framework('app/public/js', 'js', with_doctype_names=True)
  95. def build_for_pages(path):
  96. """make locale files for framework py and js (all)"""
  97. messages = []
  98. for (basepath, folders, files) in os.walk(path):
  99. if os.path.basename(os.path.dirname(basepath))=="page":
  100. messages_js, messages_py = [], []
  101. for fname in files:
  102. if fname.endswith('.js'):
  103. messages_js += get_message_list(os.path.join(basepath, fname))
  104. if fname.endswith('.py'):
  105. messages_py += get_message_list(os.path.join(basepath, fname))
  106. if messages_js:
  107. write_messages_file(basepath, messages_js, "js")
  108. if messages_py:
  109. write_messages_file(basepath, messages_py, "py")
  110. def build_from_database():
  111. """make doctype labels, names, options, descriptions"""
  112. def get_select_options(doc):
  113. if doc.doctype=="DocField" and doc.fieldtype=='Select' and doc.options \
  114. and not doc.options.startswith("link:") \
  115. and not doc.options.startswith("attach_files:"):
  116. return doc.options.split('\n')
  117. else:
  118. return []
  119. build_for_doc_from_database(webnotes._dict({
  120. "doctype": "DocType",
  121. "module_field": "module",
  122. "DocType": ["name", "description", "module"],
  123. "DocField": ["label", "description"],
  124. "custom": get_select_options
  125. }))
  126. def build_for_doc_from_database(fields):
  127. for item in webnotes.conn.sql("""select name from `tab%s`""" % fields.doctype, as_dict=1):
  128. messages = []
  129. doclist = webnotes.bean(fields.doctype, item.name).doclist
  130. for doc in doclist:
  131. if doc.doctype in fields:
  132. messages += map(lambda x: x in fields[doc.doctype] and doc.fields.get(x) or None,
  133. doc.fields.keys())
  134. if fields.custom:
  135. messages += fields.custom(doc)
  136. doc = doclist[0]
  137. if doc.fields.get(fields.module_field):
  138. doctype_path = get_doc_path(doc.fields[fields.module_field],
  139. doc.doctype, doc.name)
  140. write_messages_file(doctype_path, messages, 'doc')
  141. def build_for_framework(path, mtype, with_doctype_names = False):
  142. """make locale files for framework py and js (all)"""
  143. messages = []
  144. for (basepath, folders, files) in os.walk(path):
  145. for fname in files:
  146. if fname.endswith('.' + mtype):
  147. messages += get_message_list(os.path.join(basepath, fname))
  148. # append module & doctype names
  149. if with_doctype_names:
  150. for m in webnotes.conn.sql("""select name, module from `tabDocType`"""):
  151. messages.append(m[0])
  152. messages.append(m[1])
  153. # append labels from config.json
  154. config = webnotes.get_config()
  155. for moduleinfo in config["modules"].values():
  156. if moduleinfo.get("label"):
  157. messages.append(moduleinfo["label"])
  158. if messages:
  159. write_messages_file(path, messages, mtype)
  160. def build_from_doctype_code(path):
  161. """walk and make locale files in all folders"""
  162. for (basepath, folders, files) in os.walk(path):
  163. messagespy = []
  164. messagesjs = []
  165. for fname in files:
  166. if fname.endswith('py'):
  167. messagespy += get_message_list(os.path.join(basepath, fname))
  168. if fname.endswith('js'):
  169. messagesjs += get_message_list(os.path.join(basepath, fname))
  170. if messagespy:
  171. write_messages_file(basepath, messagespy, 'py')
  172. if messagespy:
  173. write_messages_file(basepath, messagesjs, 'js')
  174. def get_message_list(path):
  175. """get list of messages from a code file"""
  176. import re
  177. messages = []
  178. with open(path, 'r') as sourcefile:
  179. txt = sourcefile.read()
  180. messages += re.findall('_\("([^"]*)"\)', txt)
  181. messages += re.findall("_\('([^']*)'\)", txt)
  182. messages += re.findall('_\("{3}([^"]*)"{3}\)', txt, re.S)
  183. return messages
  184. def write_messages_file(path, messages, mtype):
  185. """write messages to translation file"""
  186. if not os.path.exists(path):
  187. return
  188. if not os.path.exists(os.path.join(path, 'locale')):
  189. os.makedirs(os.path.join(path, 'locale'))
  190. fname = os.path.join(path, 'locale', '_messages_' + mtype + '.json')
  191. messages = list(set(messages))
  192. filtered = []
  193. for m in messages:
  194. if m and re.search('[a-zA-Z]+', m):
  195. filtered.append(m)
  196. with open(fname, 'w') as msgfile:
  197. msgfile.write(json.dumps(filtered, indent=1))
  198. def export_messages(lang, outfile):
  199. """get list of all messages"""
  200. messages = {}
  201. # extract messages
  202. for (basepath, folders, files) in os.walk('.'):
  203. def _get_messages(messages, basepath, mtype):
  204. mlist = get_messages(basepath, mtype)
  205. if not mlist:
  206. return
  207. # update messages with already existing translations
  208. langdata = get_lang_data(basepath, lang, mtype)
  209. for m in mlist:
  210. if not messages.get(m):
  211. messages[m] = langdata.get(m, "")
  212. if os.path.basename(basepath)=='locale':
  213. _get_messages(messages, basepath, 'doc')
  214. _get_messages(messages, basepath, 'py')
  215. _get_messages(messages, basepath, 'js')
  216. # remove duplicates
  217. if outfile:
  218. from csv import writer
  219. with open(outfile, 'w') as msgfile:
  220. w = writer(msgfile)
  221. keys = messages.keys()
  222. keys.sort()
  223. for m in keys:
  224. w.writerow([m.encode('utf-8'), messages.get(m, '').encode('utf-8')])
  225. def import_messages(lang, infile):
  226. """make individual message files for each language"""
  227. data = dict(get_all_messages_from_file(infile))
  228. for (basepath, folders, files) in os.walk('.'):
  229. def _update_lang_file(mtype):
  230. """create a langauge file for the given message type"""
  231. messages = get_messages(basepath, mtype)
  232. if not messages: return
  233. # read existing
  234. langdata = get_lang_data(basepath, lang, mtype)
  235. # update fresh
  236. for m in messages:
  237. if data.get(m):
  238. langdata[m] = data.get(m)
  239. if langdata:
  240. # write new langfile
  241. langfilename = os.path.join(basepath, lang + '-' + mtype + '.json')
  242. with open(langfilename, 'w') as langfile:
  243. langfile.write(json.dumps(langdata, indent=1, sort_keys=True).encode('utf-8'))
  244. #print 'wrote ' + langfilename
  245. if os.path.basename(basepath)=='locale':
  246. # make / update lang files for each type of message file (doc, js, py)
  247. # example: hi-doc.json, hi-js.json, hi-py.json
  248. _update_lang_file('doc')
  249. _update_lang_file('js')
  250. _update_lang_file('py')
  251. def get_doc_messages(module, doctype, name):
  252. return get_lang_data(get_doc_path(module, doctype, name), None, 'doc')
  253. def get_lang_data(basepath, lang, mtype):
  254. """get language dict from langfile"""
  255. # add "locale" folder if reqd
  256. if os.path.basename(basepath) != 'locale':
  257. basepath = os.path.join(basepath, 'locale')
  258. if not lang: lang = webnotes.lang
  259. path = os.path.join(basepath, lang + '-' + mtype + '.json')
  260. langdata = {}
  261. if os.path.exists(path):
  262. with codecs.open(path, 'r', 'utf-8') as langfile:
  263. langdata = json.loads(langfile.read())
  264. return langdata
  265. def get_messages(basepath, mtype):
  266. """load list of messages from _message files"""
  267. # get message list
  268. path = os.path.join(basepath, '_messages_' + mtype + '.json')
  269. messages = []
  270. if os.path.exists(path):
  271. with open(path, 'r') as msgfile:
  272. messages = json.loads(msgfile.read())
  273. return messages
  274. def update_lang_js(jscode, path):
  275. return jscode + "\n\n$.extend(wn._messages, %s)" % \
  276. json.dumps(get_lang_data(path, webnotes.lang, 'js'))
  277. def get_all_messages_from_file(path):
  278. with codecs.open(path, 'r', 'utf-8') as msgfile:
  279. data = msgfile.read()
  280. data = reader([r.encode('utf-8') for r in data.splitlines()])
  281. newdata = []
  282. for row in data:
  283. newrow = []
  284. for val in row:
  285. newrow.append(unicode(val, 'utf-8'))
  286. newdata.append(newrow)
  287. return newdata
  288. def google_translate(lang, infile, outfile):
  289. """translate objects using Google API. Add you own API key for translation"""
  290. data = get_all_messages_from_file(infile)
  291. import requests, conf
  292. # update existing translations
  293. if os.path.exists(outfile):
  294. with codecs.open(outfile, "r", "utf-8") as oldfile:
  295. old_data = oldfile.read()
  296. old_translations = dict(reader([r.encode('utf-8').strip() for r in old_data.splitlines()]))
  297. with open(outfile, 'w') as msgfile:
  298. from csv import writer
  299. w = writer(msgfile)
  300. for row in data:
  301. if row[0] and row[0].strip():
  302. if old_translations.get(row[0].strip()):
  303. row[1] = old_translations[row[0].strip()]
  304. else:
  305. print 'translating: ' + row[0]
  306. response = requests.get("""https://www.googleapis.com/language/translate/v2""",
  307. params = {
  308. "key": conf.google_api_key,
  309. "source": "en",
  310. "target": lang,
  311. "q": row[0]
  312. })
  313. if "error" in response.json:
  314. print response.json
  315. row[1] = response.json["data"]["translations"][0]["translatedText"]
  316. if not row[1]:
  317. row[1] = row[0] # google unable to translate!
  318. row[1] = row[1].encode('utf-8')
  319. row[0] = row[0].encode('utf-8')
  320. w.writerow(row)