2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Verifies that a given messages.json file defines all the strings used by the
7 a given set of files. For file formats where it is not possible to infer which
8 strings represent message identifiers, localized strings should be explicitly
9 annotated with the string "i18n-content", for example:
11 LocalizeString(/*i18n-content*/"PRODUCT_NAME");
13 This script also recognises localized strings in HTML and manifest.json files:
15 HTML: <span i18n-content="PRODUCT_NAME"></span>
16 or ...i18n-value-name-1="BUTTON_NAME"...
17 manifest.json: __MSG_PRODUCT_NAME__
19 Note that these forms must be exact; extra spaces are not permitted, though
20 either single or double quotes are recognized.
22 In addition, the script checks that all the messages are still in use; if
23 this is not the case then a warning is issued, but the script still succeeds.
34 To remove this warning, either remove the unused tags from
35 messages.json, add the files that use the tags listed above to
36 remoting.gyp, or annotate existing uses of those tags with the
37 prefix /*i18n-content*/
41 def ExtractTagFromLine(line
):
42 """Extract a tag from a line of HTML, C++, JS or JSON."""
44 m
= re
.search('i18n-content=[\'"]([^\'"]*)[\'"]', line
)
45 if m
: return m
.group(1)
46 # HTML-style (substitutions)
47 m
= re
.search('i18n-value-name-[1-9]=[\'"]([^\'"]*)[\'"]', line
)
48 if m
: return m
.group(1)
49 # C++/Javascript style
50 m
= re
.search('/\*i18n-content\*/[\'"]([^\`"]*)[\'"]', line
)
51 if m
: return m
.group(1)
53 m
= re
.search('__MSG_(.*)__', line
)
54 if m
: return m
.group(1)
58 def CheckFileForUnknownTag(filename
, messages
):
59 """Parse |filename|, looking for tags and report any that are not included in
60 |messages|. Return True if all tags are present and correct, or False if
61 any are missing. If no tags are found, print a warning message and return
65 f
= open(filename
, 'r')
67 for i
in xrange(0, len(lines
)):
68 tag
= ExtractTagFromLine(lines
[i
])
72 if not tag
in messages
:
74 print '%s/%s:%d: error: Undefined tag: %s' % \
75 (os
.getcwd(), filename
, i
+ 1, tag
)
77 print '%s/%s:0: warning: No tags found' % (os
.getcwd(), filename
)
84 print 'Usage: verify-webapp.py <touch> <messages> <message_users...>'
87 touch_file
= sys
.argv
[1]
88 messages
= json
.load(open(sys
.argv
[2], 'r'))
90 for f
in sys
.argv
[3:]:
91 if not CheckFileForUnknownTag(f
, messages
):
96 if tag
not in all_tags
:
97 print ('%s/%s:0: warning: %s is defined but not used') % \
98 (os
.getcwd(), sys
.argv
[2], tag
)
101 print WARNING_MESSAGE
104 f
= open(touch_file
, 'a')
106 os
.utime(touch_file
, None)
111 if __name__
== '__main__':