1 : # -*- perl -*- vim: ft=perl
2 eval 'exec perl -w -S $0 ${1+"$@"}'
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 # See Usage() below or invoke without arguments for short instructions.
23 # For long instructions use the source, Luke ;-)
31 "langid - a hackish utility to lookup lang.h language defines and LangIDs,\n",
32 "isolang.cxx ISO639/ISO3166 mapping, locale data files, langtab.hrc language\n",
33 "listbox entries, langlist.mk, file_ooo.scp registry name, languages.pm and\n",
34 "msi-encodinglist.txt\n\n",
36 "Usage: $0 [--single] {language string} | {LangID} | {primarylanguage sublanguage} | {language-country}\n\n",
38 "A language string will be used as a generic string match in all searched files.\n",
39 "You may enclose the language string in word delimiters,\n",
40 "e.g. \\blanguage_german\\b for a specific match.\n",
41 "If the language string expression matches more than one define,\n",
42 "e.g. as in 'german', all matching defines will be processed.\n",
43 "If the language string does not match a define or an identifier in\n",
44 "langtab.hrc, a generic string match of the listbox entries will be tried.\n\n",
46 "Numeric values of LangID,primarylanguage,sublanguage can be given\n",
47 "decimal, hexadecimal (leading 0x), octal (leading 0) or binary (leading 0b).\n",
48 "The exact language_define of an exact match will be used in remaining lookups.\n\n",
50 "A language-country pair will lookup a xx-YY mapping from isolang.cxx,\n",
51 "for example: 'en-US' or 'de-' or '-CH',\n",
52 "xx and YY can be given case insensitive, will be lowered-uppered internally,\n",
53 "and xx and YY themselves may be regular expressions.\n",
54 "Also here a list of matches will be processed.\n\n",
56 "If option --single is given, only the first match will be processed.\n\n";
59 my $SRC_ROOT = $ENV{"SRC_ROOT"};
60 if (!defined($SRC_ROOT))
62 print "\nNeed \$SRC_ROOT, please set your LibreOffice environment!\n";
67 my $LANGUAGE_MASK_PRIMARY = 0x03ff;
69 sub getPrimaryLanguage
($)
72 return $lcid & $LANGUAGE_MASK_PRIMARY;
84 return ($sub << 10) | $pri;
88 # Note that a regex needs a duplicated pair of backslashes to produce a literal
89 # \\ like in \\\\* to search for zero or more \ backslashes.
90 # @addregex can be an optional "block to grep" definition
91 # (regex-to-start-block, regex-to-end-block, regex-to-find-in-block)
94 my( $regex, $path, $module, $name, $printmsg, @addregex) = @_;
99 # Try module under current working directory first to catch local
101 my $file = "./$module/$name";
102 if (!($found = open( IN
, $file)))
104 # Then with the given path.
105 $file = "$path/$module/$name";
106 if (!($found = open( IN
, $file)))
114 while (my $line = <IN
>)
116 if ($line =~ /$regex/)
125 push( @result, $line);
129 # By convention first element is opener, second element is closer.
132 if ($line =~ /$addregex[0]/)
135 $arecloser = $addregex[1];
140 for (my $i = 2; $i < @addregex; ++$i)
142 if ($line =~ /$addregex[$i]/)
151 push( @result, $line);
154 if ($line =~ /$arecloser/)
163 if (!$found && $printmsg) {
164 print "Not found in $file\n";
165 #print "Not found in $file for $regex @addregex\n";
173 my( $lcid, @parts, $grepdef, $options, $single);
176 for ($options = 0; $options < @ARGV && $ARGV[$options] =~ /^--/; ++$options)
178 if ($ARGV[$options] eq '--single') { $single = 1; }
179 else { print "Unknown option: $ARGV[$options]\n"; }
181 if (@ARGV == 1 + $options)
183 # 0x hex, 0b bin, 0 oct
184 if ($ARGV[$options] =~ /^0/) {
185 $lcid = oct( $ARGV[0]); }
186 elsif ($ARGV[$options] =~ /^[0-9]/) {
187 $lcid = $ARGV[$options]; }
190 $grepdef = $ARGV[$options];
193 $parts[0] = getPrimaryLanguage
( $lcid);
194 $parts[1] = getSubLanguage
( $lcid);
196 elsif (@ARGV == 2 + $options)
198 for (my $i = $options; $i < 2 + $options; ++$i)
200 if ($ARGV[$i] =~ /^0/) {
201 $parts[$i] = oct( $ARGV[$i]); }
203 $parts[$i] = $ARGV[$i]; }
205 $lcid = makeLangID
( $parts[1], $parts[0]);
212 my $modifier = "(?i)";
213 my (@resultlist, @greplist, $result);
214 # If no string was given on the command line, but value(s) were, lookup the
215 # LangID value to obtain the define identifier.
218 # #define LANGUAGE_AFRIKAANS LanguageType(0x0436)
219 @resultlist = grepFile
(
220 $modifier . '^\s*#\s*define\s+[A-Z_]*' . $grepdef,
221 "$SRC_ROOT", "include", "i18nlangtag/lang.h", 1, ());
225 printf( "LangID: 0x%04X (dec %d), primary: 0x%03x, sub 0x%02x\n", $lcid,
226 $lcid, $parts[0], $parts[1]);
227 my $buf = sprintf( "0x%04X", $lcid);
228 # #define LANGUAGE_AFRIKAANS LanguageType(0x0436)
229 @resultlist = grepFile
(
230 '^\s*#\s*define\s+\w+\s+LanguageType\(' . $buf . '\)',
231 "$SRC_ROOT", "include", "i18nlangtag/lang.h", 1, ());
233 for $result (@resultlist)
235 # #define LANGUAGE_AFRIKAANS LanguageType(0x0436)
236 if ($result =~ /^\s*#\s*define\s+(\w+)\s+LanguageType\((0x[0-9a-fA-F]+)\)/)
238 push( @greplist, '\b' . $1 . '\b');
239 $modifier = ""; # complete identifier now case sensitive
244 # If the string given is of the form xx-yy lookup a language,country pair
245 # to obtain the define identifier. xx and yy may themselves be regexps.
246 # xx- is a short form for 'xx-.*' and -yy a short form for '.*-yy'
247 # Note that -Latn for '.*-Latn' also works, accidentally.
248 if ($grepdef =~ /^(.*)-$/) {
249 $grepdef = $1 . "-.*"; }
250 if ($grepdef =~ /^-(.*)$/) {
251 $grepdef = ".*-" . $1; }
252 if ($grepdef =~ /^([^-]{2,3})-([^-]{2,2})$/) # catches also .*-.*
258 # { LANGUAGE_AFRIKAANS, "af", "ZA", 0 },
259 @resultlist = grepFile
(
260 '^\s*\{\s*\w+\s*,\s*"' . $lang . '"\s*,\s*"' . $coun . '"\s*,\s*\w+\s*\}\s*,',
261 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 1, ());
262 for $result (@resultlist)
264 if ($result =~ /^\s*\{\s*(\w+)\s*,\s*"(\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*\}\s*,/)
266 push( @greplist, '\b' . $1 . '\b');
267 $modifier = ""; # complete identifier now case sensitive
274 # Same for lll-Ssss or lll-Ssss-CC language tag.
275 if ($grepdef =~ /^([^-]{2,3})-([^-]{4,4})$/ || $grepdef =~ /^([^-]{2,3})-([^-]{4,4})-([^-]{2,2})$/)
280 if (!defined($coun)) {
283 $scri = ucfirst(lc($scri));
285 # { LANGUAGE_SERBIAN_LATIN_SERBIA, "sr-Latn", "RS", 0 },
286 @resultlist = grepFile
(
287 '^\s*\{\s*\w+\s*,\s*"' . $lang . '-' . $scri . '"\s*,\s*"' . $coun . '"\s*,\s*\w+\s*\}\s*,',
288 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 1, ());
289 for $result (@resultlist)
291 if ($result =~ /^\s*\{\s*(\w+)\s*,\s*"(\w+-\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*\}\s*,/)
293 push( @greplist, '\b' . $1 . '\b');
294 $modifier = ""; # complete identifier now case sensitive
301 # And for any other language tag that MUST match case.
302 if ($grepdef =~ /^[^-]+-/)
304 # { LANGUAGE_CATALAN_VALENCIAN, "ca-ES-valencia", "ES", "ca-valencia" },
305 @resultlist = grepFile
(
306 '^\s*\{\s*\w+\s*,\s*"' . $grepdef . '"\s*,\s*"(\w*)"\s*,\s*"([^"]*)"\s*\}\s*,',
307 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 1, ());
308 for $result (@resultlist)
310 if ($result =~ /^\s*\{\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*"(\w*)"\s*,\s*"([^"]*)"\s*\}\s*,/)
312 push( @greplist, '\b' . $1 . '\b');
313 $modifier = ""; # complete identifier now case sensitive
320 if (!@greplist && $grepdef) {
321 push( @greplist, $grepdef); }
322 for $grepdef (@greplist)
324 print "\nUsing: " . $grepdef . "\n";
326 # Decimal LCID, was needed for Langpack.ulf but isn't used anymore,
327 # keep just in case we'd need it again.
328 # #define LANGUAGE_AFRIKAANS 0x0436
329 @resultlist = grepFile
(
330 $modifier . '^\s*#\s*define\s+[A-Z_]*' . $grepdef,
331 "$SRC_ROOT", "include", "i18nlangtag/lang.h", 1, ());
333 for $result (@resultlist)
335 # #define LANGUAGE_AFRIKAANS LanguageType(0x0436)
336 if ($result =~ /^\s*#\s*define\s+(\w+)\s+LanguageType\((0x[0-9a-fA-F]+)\)/)
338 push( @lcidlist, oct( $2));
343 # { LANGUAGE_AFRIKAANS, "af", "ZA", 0 },
344 @resultlist = grepFile
(
345 $modifier . '^\s*\{\s*.*' . $grepdef . '.*\s*,\s*"(\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*\}\s*,',
346 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 0, ());
347 push( @allresultslist, @resultlist);
348 # { LANGUAGE_SERBIAN_LATIN_SERBIA, "sr-Latn", "RS", 0 },
349 @resultlist = grepFile
(
350 $modifier . '^\s*\{\s*.*' . $grepdef . '.*\s*,\s*"(\w+-\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*}\s*,',
351 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 0, ());
352 push( @allresultslist, @resultlist);
353 # { LANGUAGE_CATALAN_VALENCIAN, "ca-ES-valencia", "ES", "ca-valencia" },
354 @resultlist = grepFile
(
355 $modifier . '^\s*\{\s*.*' . $grepdef . '.*\s*,\s*"([^"]+)"\s*,\s*"(\w*)"\s*,\s*"([^"]*)"\s*\}\s*,',
356 "$SRC_ROOT", "i18nlangtag", "source/isolang/isolang.cxx", 0, ());
357 push( @allresultslist, @resultlist);
360 for $result (@allresultslist)
363 # { LANGUAGE_AFRIKAANS, "af", "ZA", 0 },
364 # { LANGUAGE_SERBIAN_LATIN_SERBIA, "sr-Latn", "RS", 0 },
365 if ($result =~ /^\s*\{\s*(\w+)\s*,\s*"(\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*\}\s*,/ ||
366 $result =~ /^\s*\{\s*(\w+)\s*,\s*"(\w+-\w+)"\s*,\s*"(\w+)?"\s*,\s*\w+\s*\}\s*,/)
372 $loca = $lang . "_" . $coun;
373 push( @langtaggreplist, '\b' . $lang . '\b(-' . $coun . ')?');
378 push( @langtaggreplist, '\b' . $lang . '\b');
381 # { LANGUAGE_CATALAN_VALENCIAN, "ca-ES-valencia", "ES", "ca-valencia" },
382 if ($result =~ /^\s*\{\s*(\w+)\s*,\s*"([^"]+)"\s*,\s*"(\w*)"\s*,\s*"([^"]*)"\s*\}\s*,/)
391 push( @langtaggreplist, '\b' . $lang . '\b(-' . $coun . ')?');
395 push( @langtaggreplist, '\b' . $lang . '\b');
402 my $file = "$SRC_ROOT/i18npool/source/localedata/data/$loca.xml";
403 my $found = open( LD
, $file);
406 print "Found $file:\n";
408 while (my $line = <LD
>)
410 if ($line =~ /<(Language|Country|Variant)>/) {
414 if ($line =~ /<\/(Language
|Country
|Variant
)>/) {
420 print "No $file\n"; }
424 # Find any special treatment, may need inspection then.
425 # $grepdef already has \b word delimiters.
427 $modifier . $grepdef,
428 "$SRC_ROOT", "i18nlangtag", "source/isolang/mslangid.cxx", 1, ());
430 my $module = "svtools";
431 my $name = "inc/langtab.hrc";
432 # { NC_("STR_ARR_SVT_LANGUAGE_TABLE", "Afrikaans (South Africa)") , LANGUAGE_AFRIKAANS },
434 @resultlist = grepFile
(
435 $modifier . '^\s*\{\s*NC_\(\s*"[^"]*"\s*,\s*".*"\s*\)\s*,.*' . $grepdef . '.*\}',
436 "$SRC_ROOT", $module, $name, 1, ());
440 $modifier . '^\s*\{\s*NC_\(\s*"[^"]*"\s*,\s*".*' . $grepdef . '.*"\s*\)\s*,.*\}',
441 "$SRC_ROOT", $module, $name, 1, ()); }
443 for my $langtag (@langtaggreplist)
445 # Name (xxx) = "/registry/spool/org/openoffice/Office/Common-ctl.xcu";
447 '^\s*Name\s*\(' . $langtag . '\)\s*=',
448 "$SRC_ROOT", "scp2", "source/ooo/file_ooo.scp", 1, ());
450 # completelangiso=af ar as-IN ... zu
452 '^\s*completelangiso\s*=\s*(\s*([a-z]{2,3})(-[A-Z][A-Z])?)*' . $langtag . '',
453 "$SRC_ROOT", "solenv", "inc/langlist.mk", 1,
454 # Also grep the list of tags, one per line, \ backslash continued.
455 ('^\s*completelangiso\s*=', '^\s*$', '^\s*' . $langtag . '\s*\\\\*$'));
457 # af 1252 1078 # Afrikaans
459 '^\s*' . $langtag . '',
460 "$SRC_ROOT", "l10ntools", "source/ulfconv/msi-encodinglist.txt", 1, ());
464 '^\d*:' . $langtag . '',
465 "$SRC_ROOT", "bin", "lo-xlate-lang", 1, ());