Skip to content

Update localization for the renderer. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions bin/update-localization-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash

function print_help_exit
{
printf "Usage: %s [options]\n" $(basename $0) >&2
printf " Update the renderer.pot and language .po files with translation strings from the code.\n" >&2
printf " options:\n" >&2
printf " -p|--po-update Update po files as well. By default only the renderer.pot file is updated.\n" >&2
printf " -l|--langauge Update the only given language in addition to updating the renderer.pot file.\n" >&2
printf " -h|--help Show this help.\n" >&2
exit 1
}

TEMP=$(getopt -a -o pl:h -l po-update,language:,help -n "$(basename $0)" -- "$@")

eval set -- "$TEMP"

UPDATE_PO=false
LANGUAGE=""

while [ ! "$1" = "--" ]
do
case "$1" in
-p|--po-update)
UPDATE_PO=true
shift 1
;;
-l|--language)
LANGUAGE=$2
shift 2
;;
-h|--help)
print_help_exit
;;
*)
echo "Internal error!"
exit 1
;;
esac
done

command -v xgettext.pl >/dev/null 2>&1 || {
echo >&2 "xgettext.pl needs to be installed. It is inlcuded in the perl package Locale::Maketext::Extract. Aborting.";
exit 1;
}

cd "$( dirname "${BASH_SOURCE[0]}" )/.."

LOCDIR=lib/WeBWorK/Localize

echo "Updating $LOCDIR/renderer.pot"

xgettext.pl -o $LOCDIR/renderer.pot -D lib/RenderApp -D lib/WeBWorK -D templates lib/RenderApp.pm

if $UPDATE_PO; then
find $LOCDIR -name '*.po' -exec bash -c "echo \"Updating {}\"; msgmerge -qUN {} $LOCDIR/renderer.pot" \;
elif [[ $LANGUAGE != "" && -e "$LOCDIR/$LANGUAGE.po" ]]; then
echo "Updating $LOCDIR/$LANGUAGE.po"
msgmerge -qUN $LOCDIR/$LANGUAGE.po $LOCDIR/renderer.pot
fi
17 changes: 0 additions & 17 deletions docs/make_translation_files.md

This file was deleted.

57 changes: 13 additions & 44 deletions lib/WeBWorK/Localize.pm
Original file line number Diff line number Diff line change
@@ -1,63 +1,35 @@
package WeBWorK::Localize;
use Mojo::Base 'Locale::Maketext', -strict;

use File::Spec;

use Locale::Maketext;
use Locale::Maketext::Lexicon;
use Mojo::File;

my $path = "$ENV{RENDER_ROOT}/lib/WeBWorK/Localize";
my $pattern = File::Spec->catfile($path, '*.[pm]o');
my $decode = 1;
my $encoding = undef;

eval "
package WeBWorK::Localize::I18N;
use base 'Locale::Maketext';
%WeBWorK::Localize::I18N::Lexicon = ( '_AUTO' => 1 );
Locale::Maketext::Lexicon->import({
'i-default' => [ 'Auto' ],
'*' => [ Gettext => \$pattern ],
_decode => \$decode,
_encoding => \$encoding,
});
*tense = sub { \$_[1] . ((\$_[2] eq 'present') ? 'ing' : 'ed') };

" or die "Can't process eval in WeBWorK/Localize.pm: line 35: " . $@;

package WeBWorK::Localize;

# This subroutine is shared with the safe compartment in PG to
# allow maketext() to be constructed in PG problems and macros
# It seems to be a little fragile -- possibly it breaks
# on perl 5.8.8
sub getLoc {
my $lang = shift;
my $lh = WeBWorK::Localize::I18N->get_handle($lang);
return sub { $lh->maketext(@_) };
}
Locale::Maketext::Lexicon->import({
'i-default' => ['Auto'],
'*' => [ Gettext => Mojo::File::curfile->dirname->child('Localize', '*.[pm]o')->to_string ],
_decode => 1,
_encoding => undef,
});

sub getLangHandle {
my $lang = shift;
my $lh = WeBWorK::Localize::I18N->get_handle($lang);
return $lh;
return WeBWorK::Localize->get_handle($lang);
}

# this is like [quant] but it doesn't write the number
# This is like [quant] but it doesn't write the number.
# usage: [quant,_1,<singular>,<plural>,<optional zero>]

sub plural {
my ($handle, $num, @forms) = @_;

return "" if @forms == 0;
return '' if @forms == 0;
return $forms[2] if @forms > 2 and $num == 0;

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

# this is like [quant] but it also has -1 case
# This is like [quant] but it also has -1 case.
# usage: [negquant,_1,<neg case>,<singular>,<plural>,<optional zero>]

sub negquant {
my ($handle, $num, @forms) = @_;

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

%Lexicon = ('_AUTO' => 1,);

package WeBWorK::Localize::I18N;
use base(WeBWorK::Localize);
our %Lexicon = ('_AUTO' => 1);

1;
Loading