Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 1 | " Vim script for checking .po files. |
| 2 | " |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 3 | " Go through the file and verify that: |
| 4 | " - All %...s items in "msgid" are identical to the ones in "msgstr". |
| 5 | " - An error or warning code in "msgid" matches the one in "msgstr". |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6 | |
| 7 | if 1 " Only execute this if the eval feature is available. |
| 8 | |
| 9 | " Function to get a split line at the cursor. |
| 10 | " Used for both msgid and msgstr lines. |
| 11 | " Removes all text except % items and returns the result. |
| 12 | func! GetMline() |
| 13 | let idline = substitute(getline('.'), '"\(.*\)"$', '\1', '') |
| 14 | while line('.') < line('$') |
| 15 | + |
| 16 | let line = getline('.') |
| 17 | if line[0] != '"' |
| 18 | break |
| 19 | endif |
| 20 | let idline .= substitute(line, '"\(.*\)"$', '\1', '') |
| 21 | endwhile |
| 22 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 23 | " remove '%', not used for formatting. |
| 24 | let idline = substitute(idline, "'%'", '', 'g') |
Bram Moolenaar | 0b8cf27 | 2020-08-30 19:42:06 +0200 | [diff] [blame^] | 25 | let idline = substitute(idline, "%%", '', 'g') |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 26 | |
Bram Moolenaar | 1c6136a | 2009-09-11 11:00:05 +0000 | [diff] [blame] | 27 | " remove '%' used for plural forms. |
| 28 | let idline = substitute(idline, '\\nPlural-Forms: .\+;\\n', '', '') |
| 29 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 30 | " remove everything but % items. |
| 31 | return substitute(idline, '[^%]*\(%[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=', '\1', 'g') |
| 32 | endfunc |
| 33 | |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 34 | " This only works when 'wrapscan' is not set. |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 35 | let s:save_wrapscan = &wrapscan |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 36 | set nowrapscan |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 38 | " Start at the first "msgid" line. |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 39 | let wsv = winsaveview() |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 40 | 1 |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 41 | /^msgid\> |
| 42 | |
| 43 | " When an error is detected this is set to the line number. |
| 44 | " Note: this is used in the Makefile. |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 45 | let error = 0 |
| 46 | |
| 47 | while 1 |
Bram Moolenaar | e53ec39 | 2019-11-16 18:49:50 +0100 | [diff] [blame] | 48 | let lnum = line('.') |
| 49 | if getline(lnum) =~ 'msgid "Text;.*;"' |
| 50 | if getline(lnum + 1) !~ '^msgstr "\([^;]\+;\)\+"' |
| 51 | echomsg 'Mismatching ; in line ' . (lnum + 1) |
| 52 | echomsg 'Did you forget the trailing semicolon?' |
| 53 | if error == 0 |
| 54 | let error = lnum + 1 |
| 55 | endif |
| 56 | endif |
| 57 | endif |
| 58 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 59 | if getline(line('.') - 1) !~ "no-c-format" |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 60 | " go over the "msgid" and "msgid_plural" lines |
| 61 | let prevfromline = 'foobar' |
| 62 | while 1 |
| 63 | let fromline = GetMline() |
| 64 | if prevfromline != 'foobar' && prevfromline != fromline |
| 65 | echomsg 'Mismatching % in line ' . (line('.') - 1) |
| 66 | echomsg 'msgid: ' . prevfromline |
| 67 | echomsg 'msgid ' . fromline |
| 68 | if error == 0 |
| 69 | let error = line('.') |
| 70 | endif |
| 71 | endif |
| 72 | if getline('.') !~ 'msgid_plural' |
| 73 | break |
| 74 | endif |
| 75 | let prevfromline = fromline |
| 76 | endwhile |
| 77 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 78 | if getline('.') !~ '^msgstr' |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 79 | echomsg 'Missing "msgstr" in line ' . line('.') |
| 80 | if error == 0 |
| 81 | let error = line('.') |
| 82 | endif |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 83 | endif |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 84 | |
| 85 | " check all the 'msgstr' lines |
| 86 | while getline('.') =~ '^msgstr' |
| 87 | let toline = GetMline() |
| 88 | if fromline != toline |
| 89 | echomsg 'Mismatching % in line ' . (line('.') - 1) |
| 90 | echomsg 'msgid: ' . fromline |
| 91 | echomsg 'msgstr: ' . toline |
| 92 | if error == 0 |
| 93 | let error = line('.') |
| 94 | endif |
| 95 | endif |
| 96 | if line('.') == line('$') |
| 97 | break |
| 98 | endif |
| 99 | endwhile |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 100 | endif |
| 101 | |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 102 | " Find next msgid. Quit when there is no more. |
| 103 | let lnum = line('.') |
| 104 | silent! /^msgid\> |
| 105 | if line('.') == lnum |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 106 | break |
| 107 | endif |
| 108 | endwhile |
| 109 | |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 110 | " Check that error code in msgid matches the one in msgstr. |
| 111 | " |
| 112 | " Examples of mismatches found with msgid "E123: ..." |
| 113 | " - msgstr "E321: ..." error code mismatch |
| 114 | " - msgstr "W123: ..." warning instead of error |
| 115 | " - msgstr "E123 ..." missing colon |
| 116 | " - msgstr "..." missing error code |
| 117 | " |
| 118 | 1 |
| 119 | if search('msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!') > 0 |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 120 | echomsg 'Mismatching error/warning code in line ' . line('.') |
| 121 | if error == 0 |
| 122 | let error = line('.') |
| 123 | endif |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 124 | endif |
| 125 | |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 126 | func! CountNl(first, last) |
| 127 | let nl = 0 |
| 128 | for lnum in range(a:first, a:last) |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 129 | let nl += count(getline(lnum), "\n") |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 130 | endfor |
| 131 | return nl |
| 132 | endfunc |
| 133 | |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 134 | " Check that the \n at the end of the msgid line is also present in the msgstr |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 135 | " line. Skip over the header. |
Bram Moolenaar | b07bbb0 | 2018-04-30 15:45:17 +0200 | [diff] [blame] | 136 | 1 |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 137 | /^"MIME-Version: |
| 138 | while 1 |
| 139 | let lnum = search('^msgid\>') |
| 140 | if lnum <= 0 |
| 141 | break |
| 142 | endif |
| 143 | let strlnum = search('^msgstr\>') |
| 144 | let end = search('^$') |
| 145 | if end <= 0 |
| 146 | let end = line('$') + 1 |
| 147 | endif |
| 148 | let origcount = CountNl(lnum, strlnum - 1) |
| 149 | let transcount = CountNl(strlnum, end - 1) |
| 150 | " Allow for a few more or less line breaks when there are 2 or more |
| 151 | if origcount != transcount && (origcount <= 2 || transcount <= 2) |
Bram Moolenaar | 9966b21 | 2017-07-28 16:46:57 +0200 | [diff] [blame] | 152 | echomsg 'Mismatching "\n" in line ' . line('.') |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 153 | if error == 0 |
| 154 | let error = lnum |
| 155 | endif |
| 156 | endif |
| 157 | endwhile |
| 158 | |
Bram Moolenaar | aaef1ba | 2017-08-01 17:40:23 +0200 | [diff] [blame] | 159 | " Check that the file is well formed according to msgfmts understanding |
| 160 | if executable("msgfmt") |
| 161 | let filename = expand("%") |
Bram Moolenaar | 01164a6 | 2017-11-02 22:58:42 +0100 | [diff] [blame] | 162 | " Newer msgfmt does not take OLD_PO_FILE_INPUT argument, must be in |
| 163 | " environment. |
| 164 | let $OLD_PO_FILE_INPUT = 'yes' |
| 165 | let a = system("msgfmt --statistics " . filename) |
Bram Moolenaar | aaef1ba | 2017-08-01 17:40:23 +0200 | [diff] [blame] | 166 | if v:shell_error != 0 |
| 167 | let error = matchstr(a, filename.':\zs\d\+\ze:')+0 |
| 168 | for line in split(a, '\n') | echomsg line | endfor |
| 169 | endif |
| 170 | endif |
| 171 | |
Bram Moolenaar | 9cfc7d8 | 2018-05-13 22:37:03 +0200 | [diff] [blame] | 172 | " Check that the plural form is properly initialized |
| 173 | 1 |
| 174 | let plural = search('^msgid_plural ', 'n') |
| 175 | if (plural && search('^"Plural-Forms: ', 'n') == 0) || (plural && search('^msgstr\[0\] ".\+"', 'n') != plural + 1) |
| 176 | if search('^"Plural-Forms: ', 'n') == 0 |
| 177 | echomsg "Missing Plural header" |
| 178 | if error == 0 |
| 179 | let error = search('\(^"[A-Za-z-_]\+: .*\\n"\n\)\+\zs', 'n') - 1 |
| 180 | endif |
| 181 | elseif error == 0 |
| 182 | let error = plural |
| 183 | endif |
| 184 | elseif !plural && search('^"Plural-Forms: ', 'n') |
| 185 | " We allow for a stray plural header, msginit adds one. |
| 186 | endif |
| 187 | |
Bram Moolenaar | d1d037e | 2018-06-24 18:04:50 +0200 | [diff] [blame] | 188 | " Check that 8bit encoding is used instead of 8-bit |
| 189 | let cte = search('^"Content-Transfer-Encoding:\s\+8-bit', 'n') |
| 190 | let ctc = search('^"Content-Type:.*;\s\+\<charset=[iI][sS][oO]_', 'n') |
| 191 | let ctu = search('^"Content-Type:.*;\s\+\<charset=utf-8', 'n') |
| 192 | if cte |
| 193 | echomsg "Content-Transfer-Encoding should be 8bit instead of 8-bit" |
| 194 | " TODO: make this an error |
| 195 | " if error == 0 |
| 196 | " let error = cte |
| 197 | " endif |
| 198 | elseif ctc |
| 199 | echomsg "Content-Type charset should be 'ISO-...' instead of 'ISO_...'" |
| 200 | " TODO: make this an error |
| 201 | " if error == 0 |
| 202 | " let error = ct |
| 203 | " endif |
| 204 | elseif ctu |
| 205 | echomsg "Content-Type charset should be 'UTF-8' instead of 'utf-8'" |
| 206 | " TODO: make this an error |
| 207 | " if error == 0 |
| 208 | " let error = ct |
| 209 | " endif |
| 210 | endif |
| 211 | |
Bram Moolenaar | 9cfc7d8 | 2018-05-13 22:37:03 +0200 | [diff] [blame] | 212 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 213 | if error == 0 |
Bram Moolenaar | 7f93703 | 2017-07-19 14:34:42 +0200 | [diff] [blame] | 214 | " If all was OK restore the view. |
| 215 | call winrestview(wsv) |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 216 | echomsg "OK" |
| 217 | else |
Bram Moolenaar | d1d037e | 2018-06-24 18:04:50 +0200 | [diff] [blame] | 218 | " Put the cursor on the line with the error. |
Bram Moolenaar | ec42059 | 2016-08-26 15:51:53 +0200 | [diff] [blame] | 219 | exe error |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 220 | endif |
| 221 | |
Bram Moolenaar | a411e5d | 2010-08-04 15:47:08 +0200 | [diff] [blame] | 222 | let &wrapscan = s:save_wrapscan |
| 223 | unlet s:save_wrapscan |
| 224 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 225 | endif |