summaryrefslogtreecommitdiffstats
path: root/webkit/tools/build/merge_localized_strings.py
blob: ed1a501abbe92974751f4377a5576fdecba48ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# Copyright (c) 2009 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Helper script to merge DevTools' localized strings for a list of locales.

Gyp doesn't have any built-in looping capability, so this just provides a way to
loop over a list of locales.
"""

import getopt
import os
import sys

GRIT_DIR = None
L10N_OUT_DIR = None


class Usage(Exception):
  def __init__(self, msg):
    self.msg = msg


def calc_output(locale):
  """Determine the file that will be generated for the given locale."""
  #e.g. '<(PRODUCT_DIR)/resources/inspector/l10n/localizedStrings_da.js',
  return '%s/localizedStrings_%s.js' % (L10N_OUT_DIR, locale)


def calc_inputs(locale):
  """Determine the files that need processing for the given locale."""
  inputs = []

  #e.g. '<(grit_out_dir)/inspectorStrings_da.pak'
  inputs.append('%s/inspectorStrings_%s.js' % (GRIT_DIR, locale))

  #e.g. '<(grit_out_dir)/devtoolsStrings_da.pak'
  inputs.append('%s/devtoolsStrings_%s.js' % (GRIT_DIR, locale))

  return inputs


def list_outputs(locales):
  """Print the names of files that will be generated for the given locales.

  This is to provide gyp the list of output files, so build targets can
  properly track what needs to be built.
  """
  outputs = []
  for locale in locales:
    outputs.append(calc_output(locale))
  # Quote each element so filename spaces don't mess up gyp's attempt to parse
  # it into a list.
  print " ".join(['"%s"' % x for x in outputs])


def list_inputs(locales):
  """Print the names of files that will be processed for the given locales.

  This is to provide gyp the list of input files, so build targets can properly
  track their prerequisites.
  """
  inputs = []
  for locale in locales:
    inputs += calc_inputs(locale)
  # Quote each element so filename spaces don't mess up gyp's attempt to parse
  # it into a list.
  print " ".join(['"%s"' % x for x in inputs])


def merge_localized_strings(locales):
  """ Loop over and repack the given locales."""
  for locale in locales:
    inputs = []
    inputs += calc_inputs(locale)
    output = calc_output(locale)
    print 'Merging %s -> %s' % (inputs, output)
    out = open(output, 'w')
    for inp in inputs:
      out.write('\n// %s\n\n' % (inp))
      out.write(open(inp).read())
    out.close()


def main(argv=None):
  global GRIT_DIR
  global L10N_OUT_DIR

  if argv is None:
    argv = sys.argv

  short_options = 'iog:l:h'
  long_options = 'help'

  print_inputs = False
  print_outputs = False
  usage_msg = ''

  helpstr = """\
Usage:  %s [-h] [-i | -o] -g <DIR> -l <DIR> <locale> [...]
  -h, --help     Print this help, then exit.
  -i             Print the expected input file list, then exit.
  -o             Print the expected output file list, then exit.
  -g DIR         GRIT build files output directory.
  -l DIR         Output dir for l10n files.
  locale [...]   One or more locales.""" % (argv[0])

  try:
    try:
      opts, locales = getopt.getopt(argv[1:], short_options, long_options)
    except getopt.GetoptError, msg:
      raise Usage(str(msg))

    if not locales:
      usage_msg = 'Please specificy at least one locale to process.\n'

    for o, a in opts:
      if o in ('-i'):
        print_inputs = True
      elif o in ('-o'):
        print_outputs = True
      elif o in ('-g'):
        GRIT_DIR = a
      elif o in ('-l'):
        L10N_OUT_DIR = a
      elif o in ('-h', '--help'):
        print helpstr
        return 0

    if not (GRIT_DIR and L10N_OUT_DIR):
      usage_msg += 'Please specify "-g" and "-l".\n'
    if print_inputs and print_outputs:
      usage_msg += 'Please specify only one of "-i" or "-o".\n'

    if usage_msg:
      raise Usage(usage_msg)
  except Usage, err:
    sys.stderr.write(err.msg + '\n')
    sys.stderr.write(helpstr + '\n')
    return 2

  if print_inputs:
    list_inputs(locales)
    return 0

  if print_outputs:
    list_outputs(locales)
    return 0

  merge_localized_strings(locales)

if __name__ == '__main__':
  sys.exit(main(sys.argv))