Skip to content

Commit

Permalink
Item14156: add support for a WebLexicon
Browse files Browse the repository at this point in the history
- If present in the current web, read translations from a WebLexicon.
- Add a plugin API for other plugins to translate strings that way.
  • Loading branch information
MichaelDaum committed Sep 2, 2016
1 parent 17c6771 commit 1516587
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
14 changes: 7 additions & 7 deletions .gitignore
@@ -1,8 +1,8 @@
*.swp
MultiLingualPlugin.md5
MultiLingualPlugin.sha1
MultiLingualPlugin.tgz
MultiLingualPlugin.txt
MultiLingualPlugin.zip
MultiLingualPlugin_installer
MultiLingualPlugin_installer.pl
/MultiLingualPlugin.md5
/MultiLingualPlugin.sha1
/MultiLingualPlugin.tgz
/MultiLingualPlugin.txt
/MultiLingualPlugin.zip
/MultiLingualPlugin_installer
/MultiLingualPlugin_installer.pl
4 changes: 3 additions & 1 deletion data/System/MultiLingualPlugin.txt
Expand Up @@ -62,7 +62,8 @@ switch to different content maintained somewhere else in your wiki:
To separate the application of translated strings from their translation you might specify a lexicon, either

* using the =lexicon= parameter to =%TRANSLATE= or
* using the =CONTENT_LEXICON= preference variable
* using the =CONTENT_LEXICON= preference variable or
* using the <nop>WebLexicon topic in the current web

The lexicon is a topic holding =%META:LEXICON= records as produced by the Foswiki:Extensions/MetaDataPlugin
and specified in the [[LexiconForm]] shipped with this plugin.
Expand Down Expand Up @@ -203,6 +204,7 @@ in his/her browser or the value of ={DefaultLanguage}= as configured to this plu
%$DEPENDENCIES%

---++ Change History
| 02 Sep 2016 | added support for <nop>WebLexicon |
| 31 May 2016 | added =arg&lt;N>= way of specifying arguments to =%TRANSLATE= |
| 8 Mar 2016 | fixed error using latest Locale::Country |
| 17 Jul 2015 | fixed detection of CONTENT_LEXICON and extraction of correct string for a given language |
Expand Down
25 changes: 20 additions & 5 deletions lib/Foswiki/Plugins/MultiLingualPlugin.pm
Expand Up @@ -21,15 +21,15 @@ use warnings;
use Foswiki::Func ();
use Foswiki::Plugins ();

our $VERSION = '2.30';
our $RELEASE = '31 May 2016';
our $VERSION = '3.00';
our $RELEASE = '02 Sep 2016';
our $SHORTDESCRIPTION = 'Support for a multi lingual Foswiki';
our $NO_PREFS_IN_TOPIC = 1;
our $core;

sub initPlugin {

Foswiki::Func::registerTagHandler('LANGUAGES', sub { return getCore()->LANGUAGES(@_); });
Foswiki::Func::registerTagHandler('LANGUAGES', sub { return getCore(shift)->LANGUAGES(@_); });
Foswiki::Func::registerTagHandler('DEFAULTLANGUAGE', sub {
my $session = shift;
if ($Foswiki::cfg{MultiLingualPlugin}{SyncUserInterface}) {
Expand All @@ -38,7 +38,7 @@ sub initPlugin {
return $Foswiki::cfg{MultiLingualPlugin}{DefaultLanguage} || 'en';
}
});
Foswiki::Func::registerTagHandler('TRANSLATE', sub { return getCore()->TRANSLATE(@_); });
Foswiki::Func::registerTagHandler('TRANSLATE', sub { return getCore(shift)->TRANSLATE(@_); });

return 1;
}
Expand Down Expand Up @@ -80,10 +80,25 @@ sub getCore {

unless (defined $core) {
require Foswiki::Plugins::MultiLingualPlugin::Core;
$core = new Foswiki::Plugins::MultiLingualPlugin::Core();
$core = new Foswiki::Plugins::MultiLingualPlugin::Core(shift);
}

return $core;
}

sub translate {
my ($text, $web, $topic) = @_;

return "" unless defined $text && $text ne "";

my $params = {
_DEFAULT => $text,
};

$web ||= $Foswiki::Plugins::SESSION->{webName};
$topic ||= $Foswiki::Plugins::SESSION->{topicName};

return getCore()->TRANSLATE($params, $topic, $web);
}

1;
30 changes: 18 additions & 12 deletions lib/Foswiki/Plugins/MultiLingualPlugin/Core.pm
Expand Up @@ -19,6 +19,7 @@ use strict;
use warnings;

use Foswiki::Func ();
use Foswiki::Plugins ();
use Locale::Language;
use Locale::Country;
use Error qw(:try);
Expand All @@ -32,9 +33,12 @@ sub writeDebug {

sub new {
my $class = shift;
my $session = shift || $Foswiki::Plugins::SESSION;

my $this = bless({ @_ }, $class);

$this->{session} = $session;

if (defined $Foswiki::cfg{MultiLingualPlugin}{Aliases} && !$doneAliases) {
foreach my $key (keys %{$Foswiki::cfg{MultiLingualPlugin}{Aliases}}) {
my $val = $Foswiki::cfg{MultiLingualPlugin}{Aliases}{$key};
Expand Down Expand Up @@ -105,7 +109,7 @@ sub preProcessMaketextParams {

#print STDERR "method='$method'\n";
throw Error::Simple("invalid method $method")
unless $method =~ /^(_\*|_\-?\d+|\*|\#|quant|numf|sprintf)$/;
unless $method =~ /^(_\*|_\-?\d+|\*|\#|quant|numf|sprintf|numerate)$/;
}

push @c, $chunk;
Expand Down Expand Up @@ -138,9 +142,7 @@ sub preProcessMaketextParams {
}

sub TRANSLATE {
my ($this, $session, $params, $theTopic, $theWeb) = @_;

my $request = Foswiki::Func::getRequestObject();
my ($this, $params, $theTopic, $theWeb) = @_;

# param
my $langCode = $params->{language};
Expand All @@ -158,7 +160,7 @@ sub TRANSLATE {
}

# i18n
$langCode = $session->i18n->language()
$langCode = $this->{session}->i18n->language()
if !defined($langCode) || $langCode eq '';

$langCode = lc($langCode);
Expand All @@ -178,22 +180,26 @@ sub TRANSLATE {
$lexiconTopics = Foswiki::Func::getPreferencesValue("CONTENT_LEXICON", $theWeb)
if !defined($lexiconTopics) || $lexiconTopics eq '';

#print STDERR "translate $text, web=$theWeb, topic=$theTopic, lexiconTopics=".($lexiconTopics||'')."\n";
$lexiconTopics = 'WebLexicon'
if (!defined($lexiconTopics) || $lexiconTopics eq '')
&& Foswiki::Func::topicExists($theWeb, 'WebLexicon');

if (defined $lexiconTopics && $lexiconTopics ne "") {
$lexiconTopics = Foswiki::Func::expandCommonVariables($lexiconTopics, $theTopic, $theWeb);
my $lexiconWeb = $theWeb;
my $languageName = getLanguageOfCode($langCode);
foreach my $lexiconTopic (split(/\s*,\s*/, $lexiconTopics)) {
($lexiconWeb, $lexiconTopic) = Foswiki::Func::normalizeWebTopicName($lexiconWeb, $lexiconTopic);
#print STDERR "... reading lexicon $lexiconWeb.$lexiconTopic\n";
my $entry = $this->getLexiconEntry($lexiconWeb, $lexiconTopic, $text);
my $languageName = getLanguageOfCode($langCode);
my $translation;
if ($entry && $languageName) {
my $key = fieldTitle2FieldName("$languageName ($langCode)");
$translation = $entry->{$key} if defined $entry->{$key} && $entry->{$key} ne '';
}
return $translation if defined $translation && $translation ne "";
if (defined $translation && $translation ne "") {
$text = $translation;
last;
}
}
}

Expand All @@ -215,7 +221,7 @@ sub TRANSLATE {
my $error;
try {
$text = preProcessMaketextParams($text);
$text = $session->i18n->maketext($text, @args);
$text = $this->{session}->i18n->maketext($text, @args);

# backwards compatibility
$text =~ s/&&/\&/g;
Expand All @@ -234,7 +240,7 @@ sub TRANSLATE {

# an improvement over the core LANGUAGES macro
sub LANGUAGES {
my ($this, $session, $params, $theTopic, $theWeb) = @_;
my ($this, $params, $theTopic, $theWeb) = @_;

my $languages = $params->{_DEFAULT};
my $format = $params->{format};
Expand All @@ -253,7 +259,7 @@ sub LANGUAGES {
my $include = $params->{include};
my $exclude = $params->{exclude};

my $enabledLanguages = $session->i18n->enabled_languages();
my $enabledLanguages = $this->{session}->i18n->enabled_languages();

my @records = ();
if (defined $languages) {
Expand Down

0 comments on commit 1516587

Please sign in to comment.