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