Skip to content

Commit 2d5ac98

Browse files
authored
Merge pull request #30 from drgrice1/localization-update
Update localization for the renderer.
2 parents c226dd1 + 4f8d521 commit 2d5ac98

File tree

9 files changed

+251
-707
lines changed

9 files changed

+251
-707
lines changed

bin/update-localization-files

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
3+
function print_help_exit
4+
{
5+
printf "Usage: %s [options]\n" $(basename $0) >&2
6+
printf " Update the renderer.pot and language .po files with translation strings from the code.\n" >&2
7+
printf " options:\n" >&2
8+
printf " -p|--po-update Update po files as well. By default only the renderer.pot file is updated.\n" >&2
9+
printf " -l|--langauge Update the only given language in addition to updating the renderer.pot file.\n" >&2
10+
printf " -h|--help Show this help.\n" >&2
11+
exit 1
12+
}
13+
14+
TEMP=$(getopt -a -o pl:h -l po-update,language:,help -n "$(basename $0)" -- "$@")
15+
16+
eval set -- "$TEMP"
17+
18+
UPDATE_PO=false
19+
LANGUAGE=""
20+
21+
while [ ! "$1" = "--" ]
22+
do
23+
case "$1" in
24+
-p|--po-update)
25+
UPDATE_PO=true
26+
shift 1
27+
;;
28+
-l|--language)
29+
LANGUAGE=$2
30+
shift 2
31+
;;
32+
-h|--help)
33+
print_help_exit
34+
;;
35+
*)
36+
echo "Internal error!"
37+
exit 1
38+
;;
39+
esac
40+
done
41+
42+
command -v xgettext.pl >/dev/null 2>&1 || {
43+
echo >&2 "xgettext.pl needs to be installed. It is inlcuded in the perl package Locale::Maketext::Extract. Aborting.";
44+
exit 1;
45+
}
46+
47+
cd "$( dirname "${BASH_SOURCE[0]}" )/.."
48+
49+
LOCDIR=lib/WeBWorK/Localize
50+
51+
echo "Updating $LOCDIR/renderer.pot"
52+
53+
xgettext.pl -o $LOCDIR/renderer.pot -D lib/Renderer -D lib/WeBWorK -D templates lib/Renderer.pm
54+
55+
if $UPDATE_PO; then
56+
find $LOCDIR -name '*.po' -exec bash -c "echo \"Updating {}\"; msgmerge -qUN {} $LOCDIR/renderer.pot" \;
57+
elif [[ $LANGUAGE != "" && -e "$LOCDIR/$LANGUAGE.po" ]]; then
58+
echo "Updating $LOCDIR/$LANGUAGE.po"
59+
msgmerge -qUN $LOCDIR/$LANGUAGE.po $LOCDIR/renderer.pot
60+
fi

docs/make_translation_files.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/WeBWorK/Localize.pm

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,35 @@
11
package WeBWorK::Localize;
2+
use Mojo::Base 'Locale::Maketext', -strict;
23

3-
use File::Spec;
4-
5-
use Locale::Maketext;
64
use Locale::Maketext::Lexicon;
5+
use Mojo::File;
76

8-
my $path = "$ENV{RENDER_ROOT}/lib/WeBWorK/Localize";
9-
my $pattern = File::Spec->catfile($path, '*.[pm]o');
10-
my $decode = 1;
11-
my $encoding = undef;
12-
13-
eval "
14-
package WeBWorK::Localize::I18N;
15-
use base 'Locale::Maketext';
16-
%WeBWorK::Localize::I18N::Lexicon = ( '_AUTO' => 1 );
17-
Locale::Maketext::Lexicon->import({
18-
'i-default' => [ 'Auto' ],
19-
'*' => [ Gettext => \$pattern ],
20-
_decode => \$decode,
21-
_encoding => \$encoding,
22-
});
23-
*tense = sub { \$_[1] . ((\$_[2] eq 'present') ? 'ing' : 'ed') };
24-
25-
" or die "Can't process eval in WeBWorK/Localize.pm: line 35: " . $@;
26-
27-
package WeBWorK::Localize;
28-
29-
# This subroutine is shared with the safe compartment in PG to
30-
# allow maketext() to be constructed in PG problems and macros
31-
# It seems to be a little fragile -- possibly it breaks
32-
# on perl 5.8.8
33-
sub getLoc {
34-
my $lang = shift;
35-
my $lh = WeBWorK::Localize::I18N->get_handle($lang);
36-
return sub { $lh->maketext(@_) };
37-
}
7+
Locale::Maketext::Lexicon->import({
8+
'i-default' => ['Auto'],
9+
'*' => [ Gettext => Mojo::File::curfile->dirname->child('Localize', '*.[pm]o')->to_string ],
10+
_decode => 1,
11+
_encoding => undef,
12+
});
3813

3914
sub getLangHandle {
4015
my $lang = shift;
41-
my $lh = WeBWorK::Localize::I18N->get_handle($lang);
42-
return $lh;
16+
return WeBWorK::Localize->get_handle($lang);
4317
}
4418

45-
# this is like [quant] but it doesn't write the number
19+
# This is like [quant] but it doesn't write the number.
4620
# usage: [quant,_1,<singular>,<plural>,<optional zero>]
47-
4821
sub plural {
4922
my ($handle, $num, @forms) = @_;
5023

51-
return "" if @forms == 0;
24+
return '' if @forms == 0;
5225
return $forms[2] if @forms > 2 and $num == 0;
5326

5427
# Normal case:
5528
return ($handle->numerate($num, @forms));
5629
}
5730

58-
# this is like [quant] but it also has -1 case
31+
# This is like [quant] but it also has -1 case.
5932
# usage: [negquant,_1,<neg case>,<singular>,<plural>,<optional zero>]
60-
6133
sub negquant {
6234
my ($handle, $num, @forms) = @_;
6335

@@ -70,9 +42,6 @@ sub negquant {
7042
return ($handle->numf($num) . ' ' . $handle->numerate($num, @forms));
7143
}
7244

73-
%Lexicon = ('_AUTO' => 1,);
74-
75-
package WeBWorK::Localize::I18N;
76-
use base(WeBWorK::Localize);
45+
our %Lexicon = ('_AUTO' => 1);
7746

7847
1;

0 commit comments

Comments
 (0)