Skip to content

Commit

Permalink
Add PG's own localization.
Browse files Browse the repository at this point in the history
This separates the webwork2 and PG localizations.  PG now uses its own
translation po and pot files and provides its own language handle for
maketext.  Only the language is passed in by the caller.

The translations in the PG code have been transferred from webwork2.
  • Loading branch information
drgrice1 committed Oct 5, 2023
1 parent 442667a commit 40ef286
Show file tree
Hide file tree
Showing 24 changed files with 2,804 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
libhtml-parser-perl \
libjson-perl \
libjson-xs-perl \
liblocale-maketext-lexicon-perl \
libtest2-suite-perl \
libtie-ixhash-perl \
libuuid-tiny-perl \
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
conf/pg_config.yml
*~
*.swp
*.bak

conf/pg_config.yml
cover_db/

htdocs/node_modules
Expand Down
65 changes: 65 additions & 0 deletions bin/update-localization-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

function print_help_exit
{
printf "Usage: %s [options]\n" $(basename $0) >&2
printf " Update the pg.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 pg.pot file is updated.\n" >&2
printf " -l|--langauge Update the only given language in addition to updating the pg.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

if [ -z "$PG_ROOT" ]; then
echo >&2 "You need to set the PG_ROOT environment variable. Aborting."
exit 1
fi

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;
}

LOCDIR=$PG_ROOT/lib/WeBWorK/PG/Localize

cd $LOCDIR

echo "Updating $LOCDIR/pg.pot"

xgettext.pl -o pg.pot -D -D $PG_ROOT/lib -D $PG_ROOT/macros

if $UPDATE_PO; then
find $LOCDIR -name '*.po' -exec bash -c "echo \"Updating {}\"; msgmerge -qUN {} pg.pot" \;
elif [[ $LANGUAGE != "" && -e "$LANGUAGE.po" ]]; then
echo "Updating $LOCDIR/$LANGUAGE.po"
msgmerge -qUN $LANGUAGE.po pg.pot
fi
1 change: 1 addition & 0 deletions conf/pg_config.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ modules:
- [PGresponsegroup]
- ['Tie::IxHash']
- ['Locale::Maketext']
- ['WeBWorK::PG::Localize']
- [JSON]
- ['Class::Tiny']
- ['IO::Handle']
Expand Down
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on runtime => sub {
requires 'JSON';
requires 'JSON::XS';
requires 'Locale::Maketext';
requires 'Locale::Maketext::Lexicon';
requires 'Tie::IxHash';
requires 'Types::Serialiser';
requires 'UUID::Tiny';
Expand Down
1 change: 1 addition & 0 deletions docker/pg.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN apt-get update \
libhtml-parser-perl \
libjson-perl \
libjson-xs-perl \
liblocale-maketext-lexicon-perl \
libtest2-suite-perl \
libtie-ixhash-perl \
libuuid-tiny-perl \
Expand Down
4 changes: 1 addition & 3 deletions lib/PGcore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ sub initialize {
WARNING_messages => $self->{WARNING_messages},
DEBUG_messages => $self->{DEBUG_messages},
);
#$self->{maketext} = WeBWorK::Localize::getLoc($self->{envir}->{language});
$self->{maketext} = $self->{envir}->{language_subroutine};
#$self->debug_message("PG alias created", $self->{PG_alias} );
$self->{maketext} = $self->{envir}{language_subroutine};
$self->{PG_loadMacros} = new PGloadfiles($self->{envir});
$self->{flags} = {
showPartialCorrectAnswers => 1,
Expand Down
5 changes: 3 additions & 2 deletions lib/WeBWorK/PG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use WeBWorK::PG::Environment;
use WeBWorK::PG::Translator;
use WeBWorK::PG::RestrictedClosureClass;
use WeBWorK::PG::Constants;
use WeBWorK::PG::Localize;

use constant DISPLAY_MODES => {
# display name # mode name
Expand Down Expand Up @@ -260,8 +261,8 @@ sub defineProblemEnvironment ($pg_envir, $options = {}, $image_generator = undef
mathViewLocale => $options->{mathViewLocale} // $pg_envir->{options}{mathViewLocale},

# Internationalization
language => $options->{language} // 'en',
language_subroutine => $options->{language_subroutine} // sub (@args) { return $args[0]; },
language => $options->{language} // 'en',
language_subroutine => WeBWorK::PG::Localize::getLoc($options->{language} // 'en'),

# Directories and URLs
pgMacrosDir => "$pg_envir->{directories}{root}/macros",
Expand Down
Loading

0 comments on commit 40ef286

Please sign in to comment.