PageRenderTime 57ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/.scripts/compile-translation-catalogs

https://github.com/spmurrayzzz/salt
#! | 81 lines | 65 code | 16 blank | 0 comment | 0 complexity | 544bfb6209deb3053c3bbcf832df7847 MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. :codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
  5. :copyright: Š 2013 by the SaltStack Team, see AUTHORS for more details.
  6. :license: Apache 2.0, see LICENSE for more details.
  7. compile-translation-catalogs
  8. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. Compile the existing translation catalogs.
  10. '''
  11. # Import python libs
  12. import os
  13. import sys
  14. import fnmatch
  15. # Import 3rd-party libs
  16. HAS_BABEL = False
  17. try:
  18. from babel.messages import mofile, pofile
  19. HAS_BABEL = True
  20. except ImportError:
  21. try:
  22. import polib
  23. except ImportError:
  24. print(
  25. 'You need to install either babel or pofile in order to compile '
  26. 'the message catalogs. One of:\n'
  27. ' pip install babel\n'
  28. ' pip install polib'
  29. )
  30. sys.exit(1)
  31. DOC_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
  32. LOCALES_DIR = os.path.join(DOC_DIR, 'locale')
  33. def main():
  34. '''
  35. Run the compile code
  36. '''
  37. print('Gathering the translation catalogs to compile...'),
  38. sys.stdout.flush()
  39. entries = {}
  40. for locale in os.listdir(os.path.join(LOCALES_DIR)):
  41. if locale == 'pot':
  42. continue
  43. locale_path = os.path.join(LOCALES_DIR, locale)
  44. entries[locale] = []
  45. for dirpath, _, filenames in os.walk(locale_path):
  46. for filename in fnmatch.filter(filenames, '*.po'):
  47. entries[locale].append(os.path.join(dirpath, filename))
  48. print('DONE')
  49. for locale, po_files in sorted(entries.items()):
  50. lc_messages_path = os.path.join(LOCALES_DIR, locale, 'LC_MESSAGES')
  51. print('\nCompiling the {0!r} locale:'.format(locale))
  52. for po_file in sorted(po_files):
  53. relpath = os.path.relpath(po_file, lc_messages_path)
  54. print ' {0}.po -> {0}.mo'.format(relpath.split('.po', 1)[0])
  55. if HAS_BABEL:
  56. catalog = pofile.read_po(open(po_file))
  57. mofile.write_mo(
  58. open(po_file.replace('.po', '.mo'), 'wb'), catalog
  59. )
  60. continue
  61. catalog = polib.pofile(po_file)
  62. catalog.save_as_mofile(fpath=po_file.replace('.po', '.mo'))
  63. print('Done')
  64. if __name__ == '__main__':
  65. main()