copy-locales.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # Copyright (c) 2013 GitHub, Inc.
  3. # Use of this source code is governed by the MIT license that can be
  4. # found in the LICENSE file.
  5. import errno
  6. import optparse
  7. import os
  8. import shutil
  9. import sys
  10. def main(argv):
  11. parser = optparse.OptionParser()
  12. usage = 'usage: %s [options ...] src dest locale_list'
  13. parser.set_usage(usage.replace('%s', '%prog'))
  14. parser.add_option('-d', dest='dash_to_underscore', action="store_true",
  15. default=False,
  16. help='map "en-US" to "en" and "-" to "_" in locales')
  17. (options, arglist) = parser.parse_args(argv)
  18. if len(arglist) < 4:
  19. print 'ERROR: need src, dest and list of locales'
  20. return 1
  21. src = arglist[1]
  22. dest = arglist[2]
  23. locales = arglist[3:]
  24. for locale in locales:
  25. # For Cocoa to find the locale at runtime, it needs to use '_' instead
  26. # of '-' (http://crbug.com/20441). Also, 'en-US' should be represented
  27. # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578).
  28. dirname = locale
  29. if options.dash_to_underscore:
  30. if locale == 'en-US':
  31. dirname = 'en'
  32. else:
  33. dirname = locale.replace('-', '_')
  34. dirname = os.path.join(dest, dirname + '.lproj')
  35. safe_mkdir(dirname)
  36. shutil.copy2(os.path.join(src, locale + '.pak'),
  37. os.path.join(dirname, 'locale.pak'))
  38. def safe_mkdir(path):
  39. try:
  40. os.makedirs(path)
  41. except OSError as e:
  42. if e.errno != errno.EEXIST:
  43. raise
  44. if __name__ == '__main__':
  45. sys.exit(main(sys.argv))