PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/django/core/management/commands/makemessages.py

https://github.com/insane/django
Python | 413 lines | 384 code | 17 blank | 12 comment | 36 complexity | dcaf7a42172e5cd05668c1c8226ffca2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import fnmatch
  2. import glob
  3. import os
  4. import re
  5. import sys
  6. from itertools import dropwhile
  7. from optparse import make_option
  8. import django
  9. from django.core.management.base import CommandError, NoArgsCommand
  10. from django.core.management.utils import (handle_extensions, find_command,
  11. popen_wrapper)
  12. from django.utils.functional import total_ordering
  13. from django.utils.text import get_text_list
  14. from django.utils.jslex import prepare_js_for_gettext
  15. plural_forms_re = re.compile(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', re.MULTILINE | re.DOTALL)
  16. STATUS_OK = 0
  17. def check_programs(*programs):
  18. for program in programs:
  19. if find_command(program) is None:
  20. raise CommandError("Can't find %s. Make sure you have GNU "
  21. "gettext tools 0.15 or newer installed." % program)
  22. @total_ordering
  23. class TranslatableFile(object):
  24. def __init__(self, dirpath, file_name):
  25. self.file = file_name
  26. self.dirpath = dirpath
  27. def __repr__(self):
  28. return "<TranslatableFile: %s>" % os.sep.join([self.dirpath, self.file])
  29. def __eq__(self, other):
  30. return self.dirpath == other.dirpath and self.file == other.file
  31. def __lt__(self, other):
  32. if self.dirpath == other.dirpath:
  33. return self.file < other.file
  34. return self.dirpath < other.dirpath
  35. def process(self, command, potfile, domain, keep_pot=False):
  36. """
  37. Extract translatable literals from self.file for :param domain:
  38. creating or updating the :param potfile: POT file.
  39. Uses the xgettext GNU gettext utility.
  40. """
  41. from django.utils.translation import templatize
  42. if command.verbosity > 1:
  43. command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
  44. _, file_ext = os.path.splitext(self.file)
  45. if domain == 'djangojs' and file_ext in command.extensions:
  46. is_templatized = True
  47. orig_file = os.path.join(self.dirpath, self.file)
  48. with open(orig_file) as fp:
  49. src_data = fp.read()
  50. src_data = prepare_js_for_gettext(src_data)
  51. thefile = '%s.c' % self.file
  52. work_file = os.path.join(self.dirpath, thefile)
  53. with open(work_file, "w") as fp:
  54. fp.write(src_data)
  55. args = [
  56. 'xgettext',
  57. '-d', domain,
  58. '--language=C',
  59. '--keyword=gettext_noop',
  60. '--keyword=gettext_lazy',
  61. '--keyword=ngettext_lazy:1,2',
  62. '--keyword=pgettext:1c,2',
  63. '--keyword=npgettext:1c,2,3',
  64. '--from-code=UTF-8',
  65. '--add-comments=Translators',
  66. '--output=-'
  67. ]
  68. if command.wrap:
  69. args.append(command.wrap)
  70. if command.location:
  71. args.append(command.location)
  72. args.append(work_file)
  73. elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions):
  74. thefile = self.file
  75. orig_file = os.path.join(self.dirpath, self.file)
  76. is_templatized = file_ext in command.extensions
  77. if is_templatized:
  78. with open(orig_file, "rU") as fp:
  79. src_data = fp.read()
  80. thefile = '%s.py' % self.file
  81. content = templatize(src_data, orig_file[2:])
  82. with open(os.path.join(self.dirpath, thefile), "w") as fp:
  83. fp.write(content)
  84. work_file = os.path.join(self.dirpath, thefile)
  85. args = [
  86. 'xgettext',
  87. '-d', domain,
  88. '--language=Python',
  89. '--keyword=gettext_noop',
  90. '--keyword=gettext_lazy',
  91. '--keyword=ngettext_lazy:1,2',
  92. '--keyword=ugettext_noop',
  93. '--keyword=ugettext_lazy',
  94. '--keyword=ungettext_lazy:1,2',
  95. '--keyword=pgettext:1c,2',
  96. '--keyword=npgettext:1c,2,3',
  97. '--keyword=pgettext_lazy:1c,2',
  98. '--keyword=npgettext_lazy:1c,2,3',
  99. '--from-code=UTF-8',
  100. '--add-comments=Translators',
  101. '--output=-'
  102. ]
  103. if command.wrap:
  104. args.append(command.wrap)
  105. if command.location:
  106. args.append(command.location)
  107. args.append(work_file)
  108. else:
  109. return
  110. msgs, errors, status = popen_wrapper(args)
  111. if errors:
  112. if status != STATUS_OK:
  113. if is_templatized:
  114. os.unlink(work_file)
  115. if not keep_pot and os.path.exists(potfile):
  116. os.unlink(potfile)
  117. raise CommandError(
  118. "errors happened while running xgettext on %s\n%s" %
  119. (self.file, errors))
  120. elif command.verbosity > 0:
  121. # Print warnings
  122. command.stdout.write(errors)
  123. if msgs:
  124. if is_templatized:
  125. old = '#: ' + work_file[2:]
  126. new = '#: ' + orig_file[2:]
  127. msgs = msgs.replace(old, new)
  128. write_pot_file(potfile, msgs)
  129. if is_templatized:
  130. os.unlink(work_file)
  131. def write_pot_file(potfile, msgs):
  132. """
  133. Write the :param potfile: POT file with the :param msgs: contents,
  134. previously making sure its format is valid.
  135. """
  136. if os.path.exists(potfile):
  137. # Strip the header
  138. msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
  139. else:
  140. msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
  141. with open(potfile, 'a') as fp:
  142. fp.write(msgs)
  143. class Command(NoArgsCommand):
  144. option_list = NoArgsCommand.option_list + (
  145. make_option('--locale', '-l', default=None, dest='locale', action='append',
  146. help='Creates or updates the message files for the given locale(s) (e.g. pt_BR). '
  147. 'Can be used multiple times, accepts a comma-separated list of locale names.'),
  148. make_option('--domain', '-d', default='django', dest='domain',
  149. help='The domain of the message files (default: "django").'),
  150. make_option('--all', '-a', action='store_true', dest='all',
  151. default=False, help='Updates the message files for all existing locales.'),
  152. make_option('--extension', '-e', dest='extensions',
  153. help='The file extension(s) to examine (default: "html,txt", or "js" if the domain is "djangojs"). Separate multiple extensions with commas, or use -e multiple times.',
  154. action='append'),
  155. make_option('--symlinks', '-s', action='store_true', dest='symlinks',
  156. default=False, help='Follows symlinks to directories when examining source code and templates for translation strings.'),
  157. make_option('--ignore', '-i', action='append', dest='ignore_patterns',
  158. default=[], metavar='PATTERN', help='Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.'),
  159. make_option('--no-default-ignore', action='store_false', dest='use_default_ignore_patterns',
  160. default=True, help="Don't ignore the common glob-style patterns 'CVS', '.*', '*~' and '*.pyc'."),
  161. make_option('--no-wrap', action='store_true', dest='no_wrap',
  162. default=False, help="Don't break long message lines into several lines."),
  163. make_option('--no-location', action='store_true', dest='no_location',
  164. default=False, help="Don't write '#: filename:line' lines."),
  165. make_option('--no-obsolete', action='store_true', dest='no_obsolete',
  166. default=False, help="Remove obsolete message strings."),
  167. make_option('--keep-pot', action='store_true', dest='keep_pot',
  168. default=False, help="Keep .pot file after making messages. Useful when debugging."),
  169. )
  170. help = ("Runs over the entire source tree of the current directory and "
  171. "pulls out all strings marked for translation. It creates (or updates) a message "
  172. "file in the conf/locale (in the django tree) or locale (for projects and "
  173. "applications) directory.\n\nYou must run this command with one of either the "
  174. "--locale or --all options.")
  175. requires_model_validation = False
  176. leave_locale_alone = True
  177. def handle_noargs(self, *args, **options):
  178. locale = options.get('locale')
  179. self.domain = options.get('domain')
  180. self.verbosity = int(options.get('verbosity'))
  181. process_all = options.get('all')
  182. extensions = options.get('extensions')
  183. self.symlinks = options.get('symlinks')
  184. ignore_patterns = options.get('ignore_patterns')
  185. if options.get('use_default_ignore_patterns'):
  186. ignore_patterns += ['CVS', '.*', '*~', '*.pyc']
  187. self.ignore_patterns = list(set(ignore_patterns))
  188. self.wrap = '--no-wrap' if options.get('no_wrap') else ''
  189. self.location = '--no-location' if options.get('no_location') else ''
  190. self.no_obsolete = options.get('no_obsolete')
  191. self.keep_pot = options.get('keep_pot')
  192. if self.domain not in ('django', 'djangojs'):
  193. raise CommandError("currently makemessages only supports domains "
  194. "'django' and 'djangojs'")
  195. if self.domain == 'djangojs':
  196. exts = extensions if extensions else ['js']
  197. else:
  198. exts = extensions if extensions else ['html', 'txt']
  199. self.extensions = handle_extensions(exts)
  200. if (locale is None and not process_all) or self.domain is None:
  201. raise CommandError("Type '%s help %s' for usage information." % (
  202. os.path.basename(sys.argv[0]), sys.argv[1]))
  203. if self.verbosity > 1:
  204. self.stdout.write('examining files with the extensions: %s\n'
  205. % get_text_list(list(self.extensions), 'and'))
  206. # Need to ensure that the i18n framework is enabled
  207. from django.conf import settings
  208. if settings.configured:
  209. settings.USE_I18N = True
  210. else:
  211. settings.configure(USE_I18N = True)
  212. self.invoked_for_django = False
  213. if os.path.isdir(os.path.join('conf', 'locale')):
  214. localedir = os.path.abspath(os.path.join('conf', 'locale'))
  215. self.invoked_for_django = True
  216. # Ignoring all contrib apps
  217. self.ignore_patterns += ['contrib/*']
  218. elif os.path.isdir('locale'):
  219. localedir = os.path.abspath('locale')
  220. else:
  221. raise CommandError("This script should be run from the Django Git "
  222. "tree or your project or app tree. If you did indeed run it "
  223. "from the Git checkout or your project or application, "
  224. "maybe you are just missing the conf/locale (in the django "
  225. "tree) or locale (for project and application) directory? It "
  226. "is not created automatically, you have to create it by hand "
  227. "if you want to enable i18n for your project or application.")
  228. check_programs('xgettext')
  229. potfile = self.build_pot_file(localedir)
  230. # Build po files for each selected locale
  231. locales = []
  232. if locale is not None:
  233. locales += locale.split(',') if not isinstance(locale, list) else locale
  234. elif process_all:
  235. locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
  236. locales = [os.path.basename(l) for l in locale_dirs]
  237. if locales:
  238. check_programs('msguniq', 'msgmerge', 'msgattrib')
  239. try:
  240. for locale in locales:
  241. if self.verbosity > 0:
  242. self.stdout.write("processing locale %s\n" % locale)
  243. self.write_po_file(potfile, locale)
  244. finally:
  245. if not self.keep_pot and os.path.exists(potfile):
  246. os.unlink(potfile)
  247. def build_pot_file(self, localedir):
  248. file_list = self.find_files(".")
  249. potfile = os.path.join(localedir, '%s.pot' % str(self.domain))
  250. if os.path.exists(potfile):
  251. # Remove a previous undeleted potfile, if any
  252. os.unlink(potfile)
  253. for f in file_list:
  254. try:
  255. f.process(self, potfile, self.domain, self.keep_pot)
  256. except UnicodeDecodeError:
  257. self.stdout.write("UnicodeDecodeError: skipped file %s in %s" % (f.file, f.dirpath))
  258. return potfile
  259. def find_files(self, root):
  260. """
  261. Helper method to get all files in the given root.
  262. """
  263. def is_ignored(path, ignore_patterns):
  264. """
  265. Check if the given path should be ignored or not.
  266. """
  267. filename = os.path.basename(path)
  268. ignore = lambda pattern: fnmatch.fnmatchcase(filename, pattern)
  269. return any(ignore(pattern) for pattern in ignore_patterns)
  270. dir_suffix = '%s*' % os.sep
  271. norm_patterns = [p[:-len(dir_suffix)] if p.endswith(dir_suffix) else p for p in self.ignore_patterns]
  272. all_files = []
  273. for dirpath, dirnames, filenames in os.walk(root, topdown=True, followlinks=self.symlinks):
  274. for dirname in dirnames[:]:
  275. if is_ignored(os.path.normpath(os.path.join(dirpath, dirname)), norm_patterns):
  276. dirnames.remove(dirname)
  277. if self.verbosity > 1:
  278. self.stdout.write('ignoring directory %s\n' % dirname)
  279. for filename in filenames:
  280. if is_ignored(os.path.normpath(os.path.join(dirpath, filename)), self.ignore_patterns):
  281. if self.verbosity > 1:
  282. self.stdout.write('ignoring file %s in %s\n' % (filename, dirpath))
  283. else:
  284. all_files.append(TranslatableFile(dirpath, filename))
  285. return sorted(all_files)
  286. def write_po_file(self, potfile, locale):
  287. """
  288. Creates or updates the PO file for self.domain and :param locale:.
  289. Uses contents of the existing :param potfile:.
  290. Uses mguniq, msgmerge, and msgattrib GNU gettext utilities.
  291. """
  292. args = ['msguniq', '--to-code=utf-8']
  293. if self.wrap:
  294. args.append(self.wrap)
  295. if self.location:
  296. args.append(self.location)
  297. args.append(potfile)
  298. msgs, errors, status = popen_wrapper(args)
  299. if errors:
  300. if status != STATUS_OK:
  301. raise CommandError(
  302. "errors happened while running msguniq\n%s" % errors)
  303. elif self.verbosity > 0:
  304. self.stdout.write(errors)
  305. basedir = os.path.join(os.path.dirname(potfile), locale, 'LC_MESSAGES')
  306. if not os.path.isdir(basedir):
  307. os.makedirs(basedir)
  308. pofile = os.path.join(basedir, '%s.po' % str(self.domain))
  309. if os.path.exists(pofile):
  310. with open(potfile, 'w') as fp:
  311. fp.write(msgs)
  312. args = ['msgmerge', '-q']
  313. if self.wrap:
  314. args.append(self.wrap)
  315. if self.location:
  316. args.append(self.location)
  317. args.extend([pofile, potfile])
  318. msgs, errors, status = popen_wrapper(args)
  319. if errors:
  320. if status != STATUS_OK:
  321. raise CommandError(
  322. "errors happened while running msgmerge\n%s" % errors)
  323. elif self.verbosity > 0:
  324. self.stdout.write(errors)
  325. elif not self.invoked_for_django:
  326. msgs = self.copy_plural_forms(msgs, locale)
  327. msgs = msgs.replace(
  328. "#. #-#-#-#-# %s.pot (PACKAGE VERSION) #-#-#-#-#\n" % self.domain, "")
  329. with open(pofile, 'w') as fp:
  330. fp.write(msgs)
  331. if self.no_obsolete:
  332. args = ['msgattrib', '-o', pofile, '--no-obsolete']
  333. if self.wrap:
  334. args.append(self.wrap)
  335. if self.location:
  336. args.append(self.location)
  337. args.append(pofile)
  338. msgs, errors, status = popen_wrapper(args)
  339. if errors:
  340. if status != STATUS_OK:
  341. raise CommandError(
  342. "errors happened while running msgattrib\n%s" % errors)
  343. elif self.verbosity > 0:
  344. self.stdout.write(errors)
  345. def copy_plural_forms(self, msgs, locale):
  346. """
  347. Copies plural forms header contents from a Django catalog of locale to
  348. the msgs string, inserting it at the right place. msgs should be the
  349. contents of a newly created .po file.
  350. """
  351. django_dir = os.path.normpath(os.path.join(os.path.dirname(django.__file__)))
  352. if self.domain == 'djangojs':
  353. domains = ('djangojs', 'django')
  354. else:
  355. domains = ('django',)
  356. for domain in domains:
  357. django_po = os.path.join(django_dir, 'conf', 'locale', locale, 'LC_MESSAGES', '%s.po' % domain)
  358. if os.path.exists(django_po):
  359. with open(django_po, 'rU') as fp:
  360. m = plural_forms_re.search(fp.read())
  361. if m:
  362. if self.verbosity > 1:
  363. self.stdout.write("copying plural forms: %s\n" % m.group('value'))
  364. lines = []
  365. found = False
  366. for line in msgs.split('\n'):
  367. if not found and (not line or plural_forms_re.search(line)):
  368. line = '%s\n' % m.group('value')
  369. found = True
  370. lines.append(line)
  371. msgs = '\n'.join(lines)
  372. break
  373. return msgs