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') |
| 25 | |
Bram Moolenaar | 1c6136a | 2009-09-11 11:00:05 +0000 | [diff] [blame^] | 26 | " remove '%' used for plural forms. |
| 27 | let idline = substitute(idline, '\\nPlural-Forms: .\+;\\n', '', '') |
| 28 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 29 | " remove everything but % items. |
| 30 | return substitute(idline, '[^%]*\(%[-+ #''.0-9*]*l\=[dsuxXpoc%]\)\=', '\1', 'g') |
| 31 | endfunc |
| 32 | |
| 33 | " Start at the first "msgid" line. |
| 34 | 1 |
| 35 | /^msgid |
| 36 | let startline = line('.') |
| 37 | let error = 0 |
| 38 | |
| 39 | while 1 |
| 40 | if getline(line('.') - 1) !~ "no-c-format" |
| 41 | let fromline = GetMline() |
| 42 | if getline('.') !~ '^msgstr' |
| 43 | echo 'Missing "msgstr" in line ' . line('.') |
| 44 | let error = 1 |
| 45 | endif |
| 46 | let toline = GetMline() |
| 47 | if fromline != toline |
| 48 | echo 'Mismatching % in line ' . (line('.') - 1) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 49 | echo 'msgid: ' . fromline |
| 50 | echo 'msgstr: ' . toline |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 51 | let error = 1 |
| 52 | endif |
| 53 | endif |
| 54 | |
| 55 | " Find next msgid. |
| 56 | " Wrap around at the end of the file, quit when back at the first one. |
| 57 | /^msgid |
| 58 | if line('.') == startline |
| 59 | break |
| 60 | endif |
| 61 | endwhile |
| 62 | |
Bram Moolenaar | 595f51c | 2008-06-09 12:46:00 +0000 | [diff] [blame] | 63 | " Check that error code in msgid matches the one in msgstr. |
| 64 | " |
| 65 | " Examples of mismatches found with msgid "E123: ..." |
| 66 | " - msgstr "E321: ..." error code mismatch |
| 67 | " - msgstr "W123: ..." warning instead of error |
| 68 | " - msgstr "E123 ..." missing colon |
| 69 | " - msgstr "..." missing error code |
| 70 | " |
| 71 | 1 |
| 72 | if search('msgid "\("\n"\)\?\([EW][0-9]\+:\).*\nmsgstr "\("\n"\)\?[^"]\@=\2\@!') > 0 |
| 73 | echo 'Mismatching error/warning code in line ' . line('.') |
| 74 | let error = 1 |
| 75 | endif |
| 76 | |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 77 | if error == 0 |
| 78 | echo "OK" |
| 79 | endif |
| 80 | |
| 81 | endif |