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
|
" Vim script for checking .po files.
"
" Go through the file and verify that:
" - All %...s items in "msgid" are identical to the ones in "msgstr".
" - An error or warning code in "msgid" matches the one in "msgstr".
if 1 " Only execute this if the eval feature is available.
" Function to get a split line at the cursor.
" Used for both msgid and msgstr lines.
" Removes all text except % items and returns the result.
func! GetMline()
let idline = substitute(getline('.'), '"\(.*\)"$', '\1', '')
while line('.') < line('$')
+
let line = getline('.')
if line[0] != '"'
break
endif
let idline .= substitute(line, '"\(.*\)"$', '\1', '')
endwhile
" remove '%', not used for formatting.
let idline = substitute(idline, "'%'", '', 'g')
" remove '%' used for plural forms.
let idline = substitute(idline, '\\nPlural-Forms: .\+;\\n', '', '')
" remove everything but % items.
return substitute(idline, '[^%]*\(%[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=', '\1', 'g')
endfunc
" This only works when 'wrapscan' is not set.
let s:save_wrapscan = &wrapscan
set nowrapscan
" Start at the first "msgid" line.
let wsv = winsaveview()
1
/^msgid\>
" When an error is detected this is set to the line number.
" Note: this is used in the Makefile.
let error = 0
while 1
if getline(line('.') - 1) !~ "no-c-format"
" go over the "msgid" and "msgid_plural" lines
let prevfromline = 'foobar'
while 1
let fromline = GetMline()
if prevfromline != 'foobar' && prevfromline != fromline
echomsg 'Mismatching % in line ' . (line('.') - 1)
echomsg 'msgid: ' . prevfromline
echomsg 'msgid ' . fromline
if error == 0
let error = line('.')
endif
endif
if getline('.') !~ 'msgid_plural'
break
endif
let prevfromline = fromline
endwhile
if getline('.') !~ '^msgstr'
echomsg 'Missing "msgstr" in line ' . line('.')
if error == 0
let error = line('.')
endif
endif
" check all the 'msgstr' lines
while getline('.') =~ '^msgstr'
let toline = GetMline()
if fromline != toline
echomsg 'Mismatching % in line ' . (line('.') - 1)
echomsg 'msgid: ' . fromline
echomsg 'msgstr: ' . toline
if error == 0
let error = line('.')
endif
endif
if line('.') == line('$')
break
endif
endwhile
endif
" Find next msgid. Quit when there is no more.
let lnum = line('.')
silent! /^msgid\>
if line('.') == lnum
break
endif
endwhile
" Check that error code in msgid matches the one in msgstr.
"
" Examples of mismatches found with msgid "E123: ..."
" - msgstr "E321: ..." error code mismatch
" - msgstr "W123: ..." warning instead of error
" - msgstr "E123 ..." missing colon
" - msgstr "..." missing error code
"
1
if search('msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!') > 0
echomsg 'Mismatching error/warning code in line ' . line('.')
if error == 0
let error = line('.')
endif
endif
func! CountNl(first, last)
let nl = 0
for lnum in range(a:first, a:last)
let nl += count(getline(lnum), "\n")
endfor
return nl
endfunc
" Check that the \n at the end of the msgid line is also present in the msgstr
" line. Skip over the header.
/^"MIME-Version:
while 1
let lnum = search('^msgid\>')
if lnum <= 0
break
endif
let strlnum = search('^msgstr\>')
let end = search('^$')
if end <= 0
let end = line('$') + 1
endif
let origcount = CountNl(lnum, strlnum - 1)
let transcount = CountNl(strlnum, end - 1)
" Allow for a few more or less line breaks when there are 2 or more
if origcount != transcount && (origcount <= 2 || transcount <= 2)
echomsg 'Mismatching "\n" in line ' . line('.')
if error == 0
let error = lnum
endif
endif
endwhile
" Check that the file is well formed according to msgfmts understanding
if executable("msgfmt")
let filename = expand("%")
let a = system("msgfmt --statistics OLD_PO_FILE_INPUT=yes " . filename)
if v:shell_error != 0
let error = matchstr(a, filename.':\zs\d\+\ze:')+0
for line in split(a, '\n') | echomsg line | endfor
endif
endif
if error == 0
" If all was OK restore the view.
call winrestview(wsv)
echomsg "OK"
else
exe error
endif
let &wrapscan = s:save_wrapscan
unlet s:save_wrapscan
endif
|