Skip to content

Commit

Permalink
Item13527: fixed detection of CONTENT_LEXICON
Browse files Browse the repository at this point in the history
... and extraction of correct string for a given language
  • Loading branch information
MichaelDaum committed Jul 17, 2015
1 parent e67c601 commit 1d8e878
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
5 changes: 3 additions & 2 deletions data/System/MultiLingualPlugin.txt
Expand Up @@ -184,7 +184,7 @@ Render a full table of all known languages:
separator="$n "
sort="label"
}%
</select
</select>

---++ Installation Instructions

Expand All @@ -196,11 +196,12 @@ Render a full table of all known languages:
-->

| Author(s): | Michael Daum|
| Copyright: | &copy; 2013-2014 Michael Daum http://michaeldaumconsulting.com |
| Copyright: | &copy; 2013-2015 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 17 Jul 2015 | fixed detection of CONTENT_LEXICON 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 |
| Dependencies: | %$DEPENDENCIES% |
Expand Down
16 changes: 7 additions & 9 deletions lib/Foswiki/Plugins/MultiLingualPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# MultiLingualPlugin is Copyright (C) 2013-2014 Michael Daum http://michaeldaumconsulting.com
# MultiLingualPlugin is Copyright (C) 2013-2015 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -21,8 +21,8 @@ use warnings;
use Foswiki::Func ();
use Foswiki::Plugins ();

our $VERSION = '2.00';
our $RELEASE = '2.00';
our $VERSION = '2.10';
our $RELEASE = '2.10';
our $SHORTDESCRIPTION = 'Support for a multi lingual Foswiki';
our $NO_PREFS_IN_TOPIC = 1;
our $core;
Expand All @@ -36,15 +36,13 @@ sub initPlugin {
return 1;
}

sub finishPlugin {
undef $core;
}

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

# clear lexicon cache
if ($meta->find("LEXICON") && $core) {
my ($lexiconWeb, $lexiconTopic) = Foswiki::Func::normalizeWebTopicName($web, $topic);
delete $core->{lexicons}{$lexiconWeb.'.'.$lexiconTopic};
}

return unless $Foswiki::cfg{MultiLingualPlugin}{SyncUserInterface};

my $contentLanguage = $meta->get('PREFERENCE', 'CONTENT_LANGUAGE');
Expand Down
42 changes: 25 additions & 17 deletions lib/Foswiki/Plugins/MultiLingualPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# MultiLingualPlugin is Copyright (C) 2013-2014 Michael Daum http://michaeldaumconsulting.com
# MultiLingualPlugin is Copyright (C) 2013-2015 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -171,20 +171,31 @@ sub TRANSLATE {

return '' unless defined $text;

my $lexiconTopic = $params->{lexicon};
$lexiconTopic = Foswiki::Func::getPreferencesValue("CONTENT_LEXICON")
if !defined($lexiconTopic) || $lexiconTopic eq '';

if (defined $lexiconTopic && $lexiconTopic ne "") {
my $entry = $this->getLexiconEntry($lexiconTopic, $text);
my $languageName = getLanguageOfCode($langCode);
if ($entry && $languageName) {
my $key = fieldTitle2FieldName("$languageName ($langCode)");
$text = $entry->{$key} if defined $entry->{$key} && $entry->{$key} ne '';
$theWeb = $params->{web} if defined $params->{web};

my $lexiconTopics = $params->{lexicon};
$lexiconTopics = Foswiki::Func::getPreferencesValue("CONTENT_LEXICON", $theWeb)
if !defined($lexiconTopics) || $lexiconTopics eq '';

#print STDERR "translate $text, web=$theWeb, topic=$theTopic, lexiconTopics=".($lexiconTopics||'')."\n";

if (defined $lexiconTopics && $lexiconTopics ne "") {
$lexiconTopics = Foswiki::Func::expandCommonVariables($lexiconTopics, $theTopic, $theWeb);
my $lexiconWeb = $theWeb;
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 "";
}
}


my $args = $params->{args};
$args = '' unless defined $args;

Expand Down Expand Up @@ -222,7 +233,7 @@ sub LANGUAGES {
$format = ' * $language' unless defined $format;

my $separator = $params->{separator};
$separator = '$n' unless defined $separator;
$separator = "\n" unless defined $separator;

my $selection = $params->{selection} || '';
$selection =~ s/\,/ /g;
Expand Down Expand Up @@ -388,10 +399,7 @@ sub getLabelOfCode {
}

sub getLexiconEntry {
my ($this, $lexiconTopic, $text) = @_;

my $baseWeb = $Foswiki::Plugins::SESSION->{webName};
my ($web, $topic) = Foswiki::Func::normalizeWebTopicName($baseWeb, $lexiconTopic);
my ($this, $web, $topic, $text) = @_;

my $lexicon = $this->{lexicons}{$web.'.'.$topic};
unless (defined $lexicon) {
Expand Down

0 comments on commit 1d8e878

Please sign in to comment.