File: l10n

package info (click to toggle)
taskd 1.1.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,580 kB
  • sloc: cpp: 13,971; python: 1,523; sh: 1,052; perl: 610; ansic: 48; makefile: 15
file content (195 lines) | stat: -rwxr-xr-x 6,634 bytes parent folder | download | duplicates (3)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python2.7
################################################################################
##
## Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################

from __future__ import print_function
from __future__ import unicode_literals

import os
import sys
import argparse
import re
import fnmatch

REFERENCE = 'eng-USA.h'

def find_localizations(source, single=''):
  '''Finds all [a-z][a-z]-[A-Z][A-Z].h files in the source tree.'''
  found = []
  for path, dirs, files in os.walk(source, topdown=True, onerror=None, followlinks=False):
    matches = map(lambda x: os.path.join(path, x),
                  fnmatch.filter(files, '[a-z][a-z][a-z]-[A-Z][A-Z][A-Z].h'))

    if single:
      # Accept both cases - if user specified es-ES.h or only es-ES
      if not single.endswith('.h'):
        single = '%s.h' % single

      for match in matches:
        if match.endswith(single) or match.endswith(REFERENCE):
          found.append(match)
    else:
      found.extend(matches)

  # Make sure REFERENCE is the first column.
  # NOTE Empty string is always sorted first than any string
  found.sort(key=lambda x: "" if x.endswith(REFERENCE) else x)

  return found

def read_file(translations, file):
  '''Reads all the localized strings from a file.'''
  translations[file] = {}
  with open(file, 'r') as fh:
    for match in re.findall(r'^\s*#define\s+(STRING_[^\s]+)(\s|\\)+"([^"]*)"', fh.read(), re.MULTILINE):
      translations[file][match[0]] = match[2]

def is_present(translations, file, string):
  '''Determines if the string is defined in a translation.'''
  return string in translations[file]

def used_in_source(source, string):
  '''Determines if the string is used in the source.'''
  command = "git grep %s %s | grep -v [a-z][a-z][a-z]-[A-Z][A-Z][A-Z].h >/dev/null 2>&1" % (string, source)
  return True if os.system(command) == 0 else False

def is_translated(translations, file, string):
  '''Determines whether the string is the same in the base version as in the
     translation, indicating work needed.'''
  if file == base:
    return True
  elif string not in translations[base]:
    return True
  elif string not in translations[file]:
    return False
  else:
    return bool(translations[file][string] != translations[base][string])

def main(args):
  '''Processes all the localized files.'''
  errors = 0
  translations = {}

  for file in args.files:
    # Verify all files exist.
    if not os.path.exists(file):
      raise Exception("Localized file '%s' not readable." % file)
    read_file(translations, file)

  strings = set()
  for file in translations:
    for string in translations[file]:
      strings.add(string)

  if len(strings) == 0:
    if not args.quiet:
      print("There are no localized strings found.")
    errors = 1

  # Get length of longest string ID.
  longest_string = len(max(strings, key=len))

  # Display info.
  if not args.quiet:
    print('Scanning in', args.source)
    print()

  # Print header line.
  files = map(lambda x: os.path.basename(x), args.files)

  if not args.quiet:
    print('%-*s' % (longest_string, 'String ID'), end='')
    print(*map(lambda x: '%10s' % x, files), sep='')
    print('-' * longest_string, ' '.join(['---------'] * len(files)))

  for string in sorted(strings):
    # assess status of 'string':
    # - clean

    line = ''
    line_errors = 0
    for file in args.files:
      message = '        '
      if is_present(translations, file, string):
        if is_translated(translations, file, string):
          message = ' Ok       '
        else:
          message = ' TODO     '
      else:
        message = ' Missing  '
        line_errors = 1

      line += message

    if args.all or line_errors != 0:
      if args.search:
        if used_in_source(args.source, string):
          if not args.quiet:
            print('%-*s' % (longest_string, string), line, sep='')
        else:
          if not args.quiet:
            print('%-*s' % (longest_string, string), line, sep='')
          line_errors = 1
      else:
        if not args.quiet:
          print('%-*s' % (longest_string, string), line, sep='')

    if line_errors:
      errors = 1

  if not args.quiet:
    print('-' * longest_string, ' '.join(['---------'] * len(files)))
    print('%-*s' % (longest_string, 'Total'), end='')
    for file in args.files:
      print('%10d' % len(translations[file]), end='')
    print()

  sys.exit(errors)

if __name__ == "__main__":
  usage="""Utility for checking localized string status across translations."""

  parser = argparse.ArgumentParser(description=usage)
  parser.add_argument('--source', action='store', required=True, help='The source code tree.')
  parser.add_argument('--single', action='store', help='Show only given localization next to reference.')
  parser.add_argument('--all', action='store_true', help='Show all string IDs.')
  parser.add_argument('--search', action='store_true', help='Search source for use.')
  parser.add_argument('--quiet', action='store_true', help='Produces no output.')
  args = parser.parse_args()

  if args.source:
    args.files = find_localizations(args.source)

    if args.single:
      args.files = find_localizations(args.source, args.single)

  base = filter(lambda x: x.endswith(REFERENCE), args.files)[0]

  try:
    main(args)
  except Exception as msg:
    print('Error:', msg)