|
| 1 | +#tests/checkPot.py |
| 2 | +#A part of NonVisual Desktop Access (NVDA) |
| 3 | +#This file is covered by the GNU General Public License. |
| 4 | +#See the file COPYING for more details. |
| 5 | +#Copyright (C) 2017 NV Access Limited |
| 6 | + |
| 7 | +"""Check a translation template (pot) for strings without translator comments. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +# Existing messages that we know don't have translator comments yet. |
| 13 | +# Ideally, all of these should get translator comments, |
| 14 | +# but this is not realistic right now. |
| 15 | +# A message should be removed from here once a translator comment is added for it. |
| 16 | +EXPECTED_MESSAGES_WITHOUT_COMMENTS = { |
| 17 | + 'Focus mode', |
| 18 | + '%s landmark', |
| 19 | + 'border', |
| 20 | + 'filler', |
| 21 | + 'line', |
| 22 | + 'data item', |
| 23 | + 'header item', |
| 24 | + 'calendar', |
| 25 | + 'video', |
| 26 | + 'audio', |
| 27 | + 'modal', |
| 28 | + 'iconified', |
| 29 | + 'editable', |
| 30 | + 'checkable', |
| 31 | + 'draggable', |
| 32 | + 'dragging', |
| 33 | + 'sorted', |
| 34 | + 'sorted ascending', |
| 35 | + 'sorted descending', |
| 36 | + 'gesture map File Error', |
| 37 | + 'text \\"%s\\" not found', |
| 38 | + 'Find Error', |
| 39 | + 'Selected %s', |
| 40 | + 'on', |
| 41 | + 'Type help(object) to get help about object.', |
| 42 | + 'Type exit() to exit the console', |
| 43 | + 'NVDA Python Console', |
| 44 | + 'Emulates pressing %s on the system keyboard', |
| 45 | + 'continuous section break', |
| 46 | + 'new column section break', |
| 47 | + 'new page section break', |
| 48 | + 'even pages section break', |
| 49 | + 'odd pages section break', |
| 50 | + 'column break', |
| 51 | + 'background pattern {pattern}', |
| 52 | + 'NVDA Speech Viewer', |
| 53 | + 'text mode', |
| 54 | + 'object mode', |
| 55 | + 'NonVisual Desktop Access', |
| 56 | + 'A free and open source screen reader for Microsoft Windows', |
| 57 | + 'Copyright (C) {years} NVDA Contributors', |
| 58 | + 'Display', |
| 59 | + 'Reports and moves the review cursor to a recent message', |
| 60 | + 'left', |
| 61 | + 'right', |
| 62 | + 'Show the Handy Tech driver configuration window.', |
| 63 | + 'Recognition failed', |
| 64 | + 'General settings', |
| 65 | + 'Change the synthesizer to be used', |
| 66 | + 'Choose the voice, rate, pitch and volume to use', |
| 67 | + 'Change reporting of mouse shape and object under mouse', |
| 68 | + 'Configure how and when the review cursor moves', |
| 69 | + 'Change reporting of objects', |
| 70 | + 'Change virtual buffers specific settings', |
| 71 | + 'Change settings of document properties', |
| 72 | + 'NVDA &web site', |
| 73 | + 'Reset all settings to saved state', |
| 74 | + 'Reset all settings to default state', |
| 75 | + 'Write the current configuration to nvda.ini', |
| 76 | + 'E&xit', |
| 77 | + 'Options', |
| 78 | + 'Error renaming profile.', |
| 79 | + 'Use this profile for:', |
| 80 | + 'This change requires administrator privileges.', |
| 81 | + 'Insufficient Privileges', |
| 82 | + 'Synthesizer Error', |
| 83 | + 'Dictionary Entry Error', |
| 84 | + 'Could not load the %s display.', |
| 85 | + 'Braille Display Error', |
| 86 | + 'word', |
| 87 | + 'Taskbar', |
| 88 | + '%s items', |
| 89 | + 'invoke', |
| 90 | + 'Desktop', |
| 91 | + 'Input Message is {title}: {message}', |
| 92 | + 'Input Message is {message}', |
| 93 | + 'Series color: {colorName} ', |
| 94 | + 'Comments', |
| 95 | + 'Endnotes', |
| 96 | + 'Even pages footer', |
| 97 | + 'Even pages header', |
| 98 | + 'First page footer', |
| 99 | + 'First page header', |
| 100 | + 'Footnotes', |
| 101 | + 'Primary footer', |
| 102 | + 'Primary header', |
| 103 | + 'Text frame', |
| 104 | +} |
| 105 | + |
| 106 | +def checkPot(fileName): |
| 107 | + """Returns the number of errors. |
| 108 | + Also prints error messages and a summary to standard output. |
| 109 | + """ |
| 110 | + errors = 0 |
| 111 | + expectedErrors = 0 |
| 112 | + unexpectedSuccesses = 0 |
| 113 | + with file(fileName, "rt") as pot: |
| 114 | + for line in pot: |
| 115 | + line = line.rstrip() |
| 116 | + if not line: |
| 117 | + # End of message. |
| 118 | + hasComment = False |
| 119 | + sourceLines = [] |
| 120 | + context = "" |
| 121 | + continue |
| 122 | + if line == 'msgid ""': |
| 123 | + # Header. |
| 124 | + continue |
| 125 | + if line.startswith("#. Translators: "): |
| 126 | + # Example: "#. Translators: a message reported in the SetRowHeader script for Microsoft Word." |
| 127 | + hasComment = True |
| 128 | + continue |
| 129 | + if line.startswith("#: "): |
| 130 | + # Example: "#: NVDAObjects\window\winword.py:1322" |
| 131 | + # Strip the "#: " prefix (3 chars). |
| 132 | + sourceLines.append(line[3:]) |
| 133 | + continue |
| 134 | + if line.startswith('msgctxt "'): |
| 135 | + context = getStringFromLine(line) |
| 136 | + continue |
| 137 | + if line.startswith("msgid "): |
| 138 | + # Get the message. |
| 139 | + if line == 'msgid ""': |
| 140 | + # Multi-line msgid. |
| 141 | + # Subsequent lines are just quoted strings which should be concatenated. |
| 142 | + msgid = "" |
| 143 | + for line in pot: |
| 144 | + if line.startswith("msgstr "): |
| 145 | + break |
| 146 | + msgid += getStringFromLine(line) |
| 147 | + else: |
| 148 | + # Single line msgid. |
| 149 | + # Example: msgid "Secure Desktop" |
| 150 | + msgid = getStringFromLine(line) |
| 151 | + if context: |
| 152 | + # The context must be considered as part of the message. |
| 153 | + message = "[{context}] {msgid}".format(context=context, msgid=msgid) |
| 154 | + else: |
| 155 | + message = msgid |
| 156 | + isExpectedError = message in EXPECTED_MESSAGES_WITHOUT_COMMENTS |
| 157 | + if not hasComment and isExpectedError: |
| 158 | + expectedErrors += 1 |
| 159 | + continue |
| 160 | + if hasComment and isExpectedError: |
| 161 | + error = ("Message has translator comment, but one wasn't expected.\n" |
| 162 | + "This is good, but please remove from EXPECTED_MESSAGES_WITHOUT_COMMENTS in tests/checkPot.py") |
| 163 | + unexpectedSuccesses += 1 |
| 164 | + elif not hasComment: |
| 165 | + errors += 1 |
| 166 | + error = "Message has no translator comment." |
| 167 | + else: |
| 168 | + continue |
| 169 | + print("{error}\n" |
| 170 | + "Source lines: {lines}\n" |
| 171 | + "Message: {message}\n" |
| 172 | + .format(error=error, lines=" ".join(sourceLines), message=message)) |
| 173 | + continue |
| 174 | + print("{errors} errors, {unexpectedSuccesses} unexpected successes, {expectedErrors} expected errors" |
| 175 | + .format(errors=errors, unexpectedSuccesses=unexpectedSuccesses, expectedErrors=expectedErrors)) |
| 176 | + return errors + unexpectedSuccesses |
| 177 | + |
| 178 | +def getStringFromLine(line): |
| 179 | + if line.startswith('"'): |
| 180 | + # The quoted string begins at the start of the line. |
| 181 | + quoted = line |
| 182 | + else: |
| 183 | + # The quoted string starts after a command. |
| 184 | + # Example: msgid "Secure Desktop" |
| 185 | + quoted = line.split(" ", 1)[1] |
| 186 | + # Strip the quotes. |
| 187 | + return quoted[1:-1] |
| 188 | + |
| 189 | +if __name__ == "__main__": |
| 190 | + # Support command line usage for quick testing. |
| 191 | + fileName = sys.argv[1] |
| 192 | + print(checkPot(fileName)) |
0 commit comments