Skip to content

Commit

Permalink
Item14738: don't use retired language codes
Browse files Browse the repository at this point in the history
... us aliases instead,
sync user interface by default,
added =SITELEXICON= preference setting,
and a few performance fixes
  • Loading branch information
MichaelDaum committed Jul 16, 2018
1 parent 66fe194 commit f0b49e4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 32 deletions.
10 changes: 6 additions & 4 deletions data/System/MultiLingualPlugin.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1515758221" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1531750625" format="1.1" version="1"}%
---+!! %TOPIC%
%FORMFIELD{"Description"}%

Expand Down Expand Up @@ -61,9 +61,10 @@ 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 =lexicon= parameter of the =%TRANSLATE= makro or
* using the <nop>WebLexicon topic in the current web or
* using the =WEBLEXICON= preference variable or
* using the <nop>WebLexicon topic in the current web
* using the =SITELEXICON= preference variable

Multiple lexicons can be specified in the =WEBLEXICON= preference variable separated by commas. Entries
are looked up in the lexicons with the given precedence, that is an entry is returned as found in the first lexicon on the list.
Expand Down Expand Up @@ -209,13 +210,14 @@ in his/her browser or the value of ={DefaultLanguage}= as configured to this plu
%$DEPENDENCIES%

---++ Change History
| 16 Jul 2018 | don't use retired language codes, us aliases instead; sync user interface by default; a few performance fixes; added =SITELEXICON= preference setting |
| 12 Jan 2018 | don't require a local <nop>WebLexicon to be registered |
| 13 Sep 2017 | better handling of enabled languages; added Greek flag to mapping; suppress Locale::Codes' error reporting to stderr |
| 16 Jan 2017 | fixed crash translating strings with certain bracket links |
| 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 |
| 17 Jul 2015 | fixed detection of =WEBLEXICON= and extraction of correct string for a given language |
| 16 Dec 2014 | added =$label_name= and =$language_name= to ease select boxes |
| 1 Nov 2013 | initial release |

Expand Down
4 changes: 2 additions & 2 deletions lib/Foswiki/Plugins/MultiLingualPlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use warnings;
use Foswiki::Func ();
use Foswiki::Plugins ();

our $VERSION = '4.00';
our $RELEASE = '12 Jan 2018';
our $VERSION = '4.10';
our $RELEASE = '16 Jul 2018';
our $SHORTDESCRIPTION = 'Support for a multi lingual Foswiki';
our $NO_PREFS_IN_TOPIC = 1;
our $core;
Expand Down
9 changes: 7 additions & 2 deletions lib/Foswiki/Plugins/MultiLingualPlugin/Config.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
# **BOOLEAN**
# If enabled then both settings - CONTENT_LANGUAGE and LANGUAGE will be kept in sync
# where the first determines the content and the latter the interface language.
$Foswiki::cfg{MultiLingualPlugin}{SyncUserInterface} = 0;
$Foswiki::cfg{MultiLingualPlugin}{SyncUserInterface} = 1;

# **STRING**
# Default language
$Foswiki::cfg{MultiLingualPlugin}{DefaultLanguage} = 'en';

# **PERL**
# Alias codes
# Alias codes, i.e. mapping retired codes to new ones
$Foswiki::cfg{MultiLingualPlugin}{Aliases} = {
"en" => "gb",
"ja" => "jp",
"el" => "gr",
"ko" => "kr",
"uk" => "ua",
"da" => "dk",
};

# **STRING**
Expand Down
70 changes: 46 additions & 24 deletions lib/Foswiki/Plugins/MultiLingualPlugin/Core.pm
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ sub TRANSLATE {
if ($langCode eq 'detect') {
$langCode = '';
} else {
$langCode = Foswiki::Func::expandCommonVariables($langCode);
#$langCode = Foswiki::Func::expandCommonVariables($langCode); DISABLED for performance reasons
}
}

Expand All @@ -171,39 +171,54 @@ sub TRANSLATE {
my $key = $langCode;
$key =~ s/-/_/g; # to be able to specify a tai translation using zh_tw="..." as a parameter

# get text
my $text = $params->{$key};

# shortcut for simple inline translations
if (defined $text && $text !~ /\[_\d+\]/) { # shortcut for simple inline translations
writeDebug("shortcut result $text");
return $text;
}

$text = $params->{_DEFAULT} unless defined $text;
$text = '' unless defined $text;

return '' if $text eq '';

$theWeb = $params->{web} if defined $params->{web};
my $lexiconWeb = $params->{web} || $theWeb;
my @lexiconTopics = ();
push @lexiconTopics, $params->{lexicon} if $params->{lexicon};

my $lexiconTopics = $params->{lexicon} || '';

$lexiconTopics = Foswiki::Func::getPreferencesValue("WEBLEXICON", $theWeb)
|| Foswiki::Func::getPreferencesValue("CONTENT_LEXICON", $theWeb)
|| ''
if $lexiconTopics eq '';
unless (@lexiconTopics) {
# add existing web lexicon in this web
push @lexiconTopics, 'WebLexicon' if Foswiki::Func::topicExists($lexiconWeb, 'WebLexicon');

# add local lexicon if it exists
$lexiconTopics = 'WebLexicon, '.$lexiconTopics if Foswiki::Func::topicExists($theWeb, 'WebLexicon');
# add web lexicon preferences
my $webLexicon = Foswiki::Func::getPreferencesValue("WEBLEXICON", $lexiconWeb)
|| Foswiki::Func::getPreferencesValue("CONTENT_LEXICON", $lexiconWeb);
push @lexiconTopics, split(/\s*,\s*/, $webLexicon) if $webLexicon;

if ($lexiconTopics ne '') {
$lexiconTopics = Foswiki::Func::expandCommonVariables($lexiconTopics, $theTopic, $theWeb);
my $lexiconWeb = $theWeb;
# add site lexicon fallback
my $siteLexicon = Foswiki::Func::getPreferencesValue("SITELEXICON") || '';
push @lexiconTopics, split(/\s*,\s*/, $siteLexicon) if $siteLexicon;
}

if (@lexiconTopics) {
my $languageName = getLanguageOfCode($langCode);
foreach my $lexiconTopic (split(/\s*,\s*/, $lexiconTopics)) {
foreach my $lexiconTopic (@lexiconTopics) {
next if $lexiconTopic eq "";
($lexiconWeb, $lexiconTopic) = Foswiki::Func::normalizeWebTopicName($lexiconWeb, $lexiconTopic);
my $entry = $this->getLexiconEntry($lexiconWeb, $lexiconTopic, $text);
my $translation;
if ($entry && $languageName) {
my $key = fieldTitle2FieldName("$languageName ($langCode)");
$translation = $entry->{$key} if defined $entry->{$key} && $entry->{$key} ne '';
}
if (defined $translation && $translation ne "") {
$text = $translation;
if ($translation eq $text) {
$text .= "\0"; # prevent translation loops
} else {
$text = $translation;
}
last;
}
}
Expand Down Expand Up @@ -241,6 +256,8 @@ sub TRANSLATE {
return "<span class='foswikiAlert'><noautolink><literal>$error</literal></noautolink></span>";
}

$text =~ s/\0//g; # remove translation token

return Foswiki::Func::decodeFormatTokens($text);
}

Expand Down Expand Up @@ -352,7 +369,7 @@ sub enabledLanguages {
if (getLanguageOfCode($code)) {
$this->{enabledLanguages}{$code} = $enabledLanguages->{$code};
} else {
#print STDERR "WARNING: $code unkown to Locale::Country\n";
print STDERR "WARNING: $code unkown to Locale::Country\n" unless $code eq 'tlh'; # warn for unknown codes except klingon
}
}

Expand Down Expand Up @@ -410,7 +427,7 @@ sub getCountryOfCode {
$code = $1;
}

return code2country($code, LOCALE_CODE_ALPHA_2, 1) || '';
return code2country($code, LOCALE_CODE_ALPHA_2) || '';
}

sub getLanguageOfCode {
Expand All @@ -420,7 +437,7 @@ sub getLanguageOfCode {
$code = $1;
}

my $lang = code2language($code, LOCALE_CODE_ALPHA_2, 1) || '';
my $lang = code2language($code, LOCALE_CODE_ALPHA_2) || '';
$lang =~ s/\(\d+\-\)//; # weed out Modern Greek (1453-)
return $lang;
}
Expand All @@ -431,7 +448,7 @@ sub getLabelOfCode {
my $label;
if ($code =~ /^(\w+)-(\w+)$/) {
$code = $1;
my ($lname, $cname) = ((code2language($1, LOCALE_CODE_ALPHA_2, 1) || ''), (code2country($2, LOCALE_CODE_ALPHA_2, 1) || ''));
my ($lname, $cname) = ((code2language($1, LOCALE_CODE_ALPHA_2) || ''), (code2country($2, LOCALE_CODE_ALPHA_2) || ''));
if ($lname && $cname) {
$label = "$lname ($cname)";
} elsif ($lname) {
Expand All @@ -442,7 +459,7 @@ sub getLabelOfCode {
$label = "$code";
}
} else {
$label = code2language($code, LOCALE_CODE_ALPHA_2, 1) || "$code";
$label = code2language($code, LOCALE_CODE_ALPHA_2) || "$code";
$label =~ s/\(\d+\-\)//; # weed out Modern Greek (1453-)
}

Expand All @@ -452,18 +469,23 @@ sub getLabelOfCode {
sub getLexiconEntry {
my ($this, $web, $topic, $text) = @_;

my $lexicon = $this->{lexicons}{$web.'.'.$topic};
my ($lexiconWeb, $lexiconTopic) = Foswiki::Func::normalizeWebTopicName($web, $topic);
$lexiconWeb =~ s/\//./g;

my $key = $lexiconWeb.'.'.$lexiconTopic;

my $lexicon = $this->{lexicons}{$key};
unless (defined $lexicon) {
$lexicon = {};
writeDebug("reading lexicon from $web.$topic");
writeDebug("reading lexicon from $key");

my ($meta) = Foswiki::Func::readTopic($web, $topic);
foreach my $entry ($meta->find("LEXICON")) {
next unless $entry->{String};
$lexicon->{$entry->{String}} = $entry;
}

$this->{lexicons}{$web.'.'.$topic} = $lexicon;
$this->{lexicons}{$key} = $lexicon;
}

return $lexicon->{$text};
Expand Down

0 comments on commit f0b49e4

Please sign in to comment.