Exemplo n.º 1
0
def do_compile_translations(target=("t", ""),
                            i18n_dir=("i", ""),
                            all=("a", False)):
    """
  Compiling all the templates in specified application.
  """
    if not target and not all:
        print_status('Please specify target.')
        sys.exit(1)
    elif target == 'kay':
        print_status('Compiling builtin languages')
        root = path.join(kay.KAY_DIR, 'i18n')
    elif all:
        targets = get_user_apps()
        for target in targets:
            do_compile_translations(target=target, i18n_dir=None, all=False)
        do_compile_translations(target=kay.PROJECT_DIR,
                                i18n_dir=None,
                                all=False)
        sys.exit(0)
    else:
        if i18n_dir:
            root = i18n_dir
        else:
            root = path.join(target, 'i18n')
        if not path.isdir(root):
            print('i18n folder missing')
            sys.exit(1)
        print_status('Compiling %s' % root)

    for domain in domains:
        for lang in listdir(root):
            folder = path.join(root, lang)
            translations = path.join(folder, 'LC_MESSAGES', domain + '.po')

            if path.isfile(translations):
                mo_file = open(translations.replace('.po', '.mo'), 'wb')
                print_status('Compiling %s ' % translations)
                f = file(translations)
                try:
                    catalog = read_po(f, locale=lang)
                finally:
                    f.close()
                # Write standard catalog
                write_mo(mo_file, catalog)
                mo_file.close()
    print_status('All done.')
Exemplo n.º 2
0
def do_compile_translations(target=("t", ""), i18n_dir=("i", ""),
                            all=("a", False)):
  """
  Compiling all the templates in specified application.
  """
  if not target and not all:
    print_status('Please specify target.')
    sys.exit(1)
  elif target == 'kay':
    print_status('Compiling builtin languages')
    root = path.join(kay.KAY_DIR, 'i18n')
  elif all:
    targets = get_user_apps()
    for target in targets:
      do_compile_translations(target=target, i18n_dir=None, all=False)
    do_compile_translations(target=kay.PROJECT_DIR, i18n_dir=None, all=False)
    sys.exit(0)
  else:
    if i18n_dir:
      root = i18n_dir
    else:
      root = path.join(target, 'i18n')
    if not path.isdir(root):
      print('i18n folder missing')
      sys.exit(1)
    print_status('Compiling %s' % root)

  for domain in domains:
    for lang in listdir(root):
      folder = path.join(root, lang)
      translations = path.join(folder, 'LC_MESSAGES', domain+'.po')

      if path.isfile(translations):
        mo_file = open(translations.replace('.po', '.mo'), 'wb')
        print_status('Compiling %s ' % translations)
        f = file(translations)
        try:
          catalog = read_po(f, locale=lang)
        finally:
          f.close()
        # Write standard catalog
        write_mo(mo_file, catalog)
        mo_file.close()
  print_status('All done.')
Exemplo n.º 3
0
def do_extract_messages(target=('t', ''), domain=('d', 'messages'),
                        i18n_dir=('i', ''), all=('a', False)):
  """
  Extract messages and create pot file.
  """
  if not domain in ('messages', 'jsmessages'):
    print_status('invalid domain.')
    sys.exit(1)
  if not target and not all:
    print_status('Please specify target.')
    sys.exit(1)
  elif target == 'kay':
    print_status('Extracting core strings')
    root = kay.KAY_DIR
  elif all:
    targets = get_user_apps()
    for target in targets:
      do_extract_messages(target=target, domain=domain, i18n_dir=None,
                          all=False)
    sys.exit(0)
  else:
    root = path.abspath(target)
    if not path.isdir(root):
      print_status('source folder missing')
      sys.exit(1)
    print_status('Extracting from %s' % root)
  if domain == 'messages':
    methods = METHODS
  else:
    methods = JSMETHODS

  catalog = Catalog(msgid_bugs_address=BUGS_ADDRESS,
                    copyright_holder=COPYRIGHT, charset='utf-8')

  def callback(filename, method, options):
    if method != 'ignore':
      print_status(strip_path(filename, root))

  option = {}
  option['extensions'] = ','.join(settings.JINJA2_EXTENSIONS)
  option.update(settings.JINJA2_ENVIRONMENT_KWARGS)
  options = {
    '**/templates/**.*': option,
    '**.html': option,
  }
  extracted = extract_from_dir(root, methods, options, KEYWORDS,
                               COMMENT_TAGS, callback=callback,
                               strip_comment_tags=True)

  for filename, lineno, message, comments in extracted:
    catalog.add(message, None, [(strip_path(filename, root), lineno)],
                auto_comments=comments)
  if not i18n_dir:
    i18n_dir = path.join(root, 'i18n')
  if not path.isdir(i18n_dir):
    makedirs(i18n_dir)

  f = file(path.join(i18n_dir, domain+'.pot'), 'w')
  try:
    write_po(f, catalog, width=79)
  finally:
    f.close()

  print_status('All done.')
Exemplo n.º 4
0
def do_update_translations(target=("t", ""), lang=("l", ""),
                           statistics=("s", False), i18n_dir=("i", ""),
                           all=("a", False)):
  """
  Update existing translations with updated pot files.
  """
  if not target and not all:
    print_status('Please specify target.')
    sys.exit(1)
  elif target == 'kay':
    print_status('Updating core strings')
    root = path.join(kay.KAY_DIR, 'i18n')
  elif all:
    targets = get_user_apps()
    for target in targets:
      do_update_translations(target=target, lang=lang, statistics=statistics,
                             i18n_dir=None, all=False)
    sys.exit(0)
  else:
    if i18n_dir:
      root = i18n_dir
    else:
      root = path.join(target, 'i18n')
    if not path.isdir(root):
      print_status('source folder missing')
      sys.exit(1)
    print_status('Updating %s' % root)

  for domain in domains:
    if lang:
      filepath = path.join(root, lang, 'LC_MESSAGES', domain+'.po')
      if not path.exists(filepath):
        print_status("%s not found, skipped." % filepath)
        continue
    try:
      f = file(path.join(root, domain+'.pot'))
    except IOError:
      print_status('Can not open file: %s, skipped.' %
                   path.join(root, domain+'.pot'))
      continue
    try:
      template = read_po(f)
    finally:
      f.close()

    po_files = []
    for lang_dir in listdir(root):
      filename = path.join(root, lang_dir, 'LC_MESSAGES', domain+'.po')
      if lang and filename != \
                         path.join(root, lang, 'LC_MESSAGES', domain+'.po'):
        continue
      if path.exists(filename):
        print_status('Updating %s' % filename, nl=False)
        locale = Locale.parse(lang_dir)
        f = file(filename)
        try:
          catalog = read_po(f, locale=locale, domain=domain)
        finally:
          f.close()
        catalog.update(template)

        # XXX: this is kinda dangerous, but as we are using a
        # revision control system anyways that shouldn't make
        # too many problems
        f = file(filename, 'w')
        try:
          write_po(f, catalog, ignore_obsolete=True,
                   include_previous=False, width=79)
        finally:
          if statistics:
            translated = fuzzy = percentage = 0
            for message in list(catalog)[1:]:
              if message.string:
                translated +=1
              if 'fuzzy' in message.flags:
                fuzzy += 1
            if len(catalog):
              percentage = translated * 100 // len(catalog)
              print_status("-> %d of %d messages (%d%%) translated" % (
                translated, len(catalog), percentage), nl=False)
              if fuzzy:
                if fuzzy == 1:
                  print_status("%d of which is fuzzy" % fuzzy, nl=False)
                else:
                  print_status("%d of which are fuzzy" % fuzzy, nl=False)
              print_status()
          else:
            print_status()
          f.close()

  print_status('All done.')
Exemplo n.º 5
0
  """
  Add new translations for specified language.
  """
  try:
    locale = Locale.parse(lang)
  except (UnknownLocaleError, ValueError), e:
    print_status("You must specify lang.")
    sys.exit(1)
  if not target and not all:
    print_status("Please specify target.")
    sys.exit(1)
  elif target == 'kay':
    i18n_dir = join(kay.KAY_DIR, 'i18n')
    add_translations(locale, i18n_dir, force)
  elif all:
    targets = get_user_apps()
    for target in targets:
      do_add_translations(target=target, lang=lang, force=force,
                          i18n_dir=None, all=False)
    sys.exit(0)
  else:
    if not i18n_dir:
      i18n_dir = join(target, 'i18n')
    add_translations(locale, i18n_dir, force)


def create_from_pot(locale, path):
  try:
    f = file(path)
  except IOError, e:
    print_status("Cant open file. Skipped %s." % path)
Exemplo n.º 6
0
   """
 Add new translations for specified language.
 """
   try:
       locale = Locale.parse(lang)
   except (UnknownLocaleError, ValueError), e:
       print_status("You must specify lang.")
       sys.exit(1)
   if not target and not all:
       print_status("Please specify target.")
       sys.exit(1)
   elif target == 'kay':
       i18n_dir = join(kay.KAY_DIR, 'i18n')
       add_translations(locale, i18n_dir, force)
   elif all:
       targets = get_user_apps()
       for target in targets:
           do_add_translations(target=target,
                               lang=lang,
                               force=force,
                               i18n_dir=None,
                               all=False)
       do_add_translations(target=kay.PROJECT_DIR,
                           lang=lang,
                           force=force,
                           i18n_dir=None,
                           all=False)
       sys.exit(0)
   else:
       if not i18n_dir:
           i18n_dir = join(target, 'i18n')
Exemplo n.º 7
0
def do_update_translations(target=("t", ""),
                           lang=("l", ""),
                           statistics=("s", False),
                           i18n_dir=("i", ""),
                           all=("a", False)):
    """
  Update existing translations with updated pot files.
  """
    if not target and not all:
        print_status('Please specify target.')
        sys.exit(1)
    elif target == 'kay':
        print_status('Updating core strings')
        root = path.join(kay.KAY_DIR, 'i18n')
    elif all:
        targets = get_user_apps()
        for target in targets:
            do_update_translations(target=target,
                                   lang=lang,
                                   statistics=statistics,
                                   i18n_dir=None,
                                   all=False)
        do_update_translations(target=kay.PROJECT_DIR,
                               lang=lang,
                               statistics=statistics,
                               i18n_dir=None,
                               all=False)
        sys.exit(0)
    else:
        if i18n_dir:
            root = i18n_dir
        else:
            root = path.join(target, 'i18n')
        if not path.isdir(root):
            print_status('source folder missing')
            sys.exit(1)
        print_status('Updating %s' % root)

    for domain in domains:
        if lang:
            filepath = path.join(root, lang, 'LC_MESSAGES', domain + '.po')
            if not path.exists(filepath):
                print_status("%s not found, skipped." % filepath)
                continue
        try:
            f = file(path.join(root, domain + '.pot'))
        except IOError:
            print_status('Can not open file: %s, skipped.' %
                         path.join(root, domain + '.pot'))
            continue
        try:
            template = read_po(f)
        finally:
            f.close()

        po_files = []
        for lang_dir in listdir(root):
            filename = path.join(root, lang_dir, 'LC_MESSAGES', domain + '.po')
            if lang and filename != \
                               path.join(root, lang, 'LC_MESSAGES', domain+'.po'):
                continue
            if path.exists(filename):
                print_status('Updating %s' % filename, nl=False)
                locale = Locale.parse(lang_dir)
                f = file(filename)
                try:
                    catalog = read_po(f, locale=locale, domain=domain)
                finally:
                    f.close()
                catalog.update(template)

                # XXX: this is kinda dangerous, but as we are using a
                # revision control system anyways that shouldn't make
                # too many problems
                f = file(filename, 'w')
                try:
                    write_po(f,
                             catalog,
                             ignore_obsolete=True,
                             include_previous=False,
                             width=79)
                finally:
                    if statistics:
                        translated = fuzzy = percentage = 0
                        for message in list(catalog)[1:]:
                            if message.string:
                                translated += 1
                            if 'fuzzy' in message.flags:
                                fuzzy += 1
                        if len(catalog):
                            percentage = translated * 100 // len(catalog)
                            print_status(
                                "-> %d of %d messages (%d%%) translated" %
                                (translated, len(catalog), percentage),
                                nl=False)
                            if fuzzy:
                                if fuzzy == 1:
                                    print_status("%d of which is fuzzy" %
                                                 fuzzy,
                                                 nl=False)
                                else:
                                    print_status("%d of which are fuzzy" %
                                                 fuzzy,
                                                 nl=False)
                            print_status()
                    else:
                        print_status()
                    f.close()

    print_status('All done.')
Exemplo n.º 8
0
def do_extract_messages(target=('t', ''),
                        domain=('d', 'messages'),
                        i18n_dir=('i', ''),
                        all=('a', False)):
    """
  Extract messages and create pot file.
  """
    if not domain in ('messages', 'jsmessages'):
        print_status('invalid domain.')
        sys.exit(1)
    if not target and not all:
        print_status('Please specify target.')
        sys.exit(1)
    elif target == 'kay':
        print_status('Extracting core strings')
        root = kay.KAY_DIR
    elif all:
        targets = get_user_apps()
        for target in targets:
            do_extract_messages(target=target,
                                domain=domain,
                                i18n_dir=None,
                                all=False)
        for template_dir in settings.TEMPLATE_DIRS:
            do_extract_messages(target=template_dir,
                                domain=domain,
                                i18n_dir=settings.I18N_DIR,
                                all=False)
        sys.exit(0)
    else:
        root = path.abspath(target)
        if not path.isdir(root):
            print_status('source folder missing')
            sys.exit(1)
        print_status('Extracting from %s' % root)
    if domain == 'messages':
        methods = METHODS
    else:
        methods = JSMETHODS

    catalog = Catalog(msgid_bugs_address=BUGS_ADDRESS,
                      copyright_holder=COPYRIGHT,
                      charset='utf-8')

    def callback(filename, method, options):
        if method != 'ignore':
            print_status(strip_path(filename, root))

    option = {}
    option['extensions'] = ','.join(settings.JINJA2_EXTENSIONS)
    option.update(settings.JINJA2_ENVIRONMENT_KWARGS)
    options = {
        '**/templates/**.*': option,
        '**.html': option,
    }
    extracted = extract_from_dir(root,
                                 methods,
                                 options,
                                 KEYWORDS,
                                 COMMENT_TAGS,
                                 callback=callback,
                                 strip_comment_tags=True)

    for filename, lineno, message, comments in extracted:
        catalog.add(message,
                    None, [(strip_path(filename, root), lineno)],
                    auto_comments=comments)
    if not i18n_dir:
        i18n_dir = path.join(root, 'i18n')
    if not path.isdir(i18n_dir):
        makedirs(i18n_dir)

    f = file(path.join(i18n_dir, domain + '.pot'), 'w')
    try:
        write_po(f, catalog, width=79)
    finally:
        f.close()

    print_status('All done.')