Item12376: improvements on %LANGUAGES
Priority: Enhancement
Current State: No Action Required
Released In: n/a
Target Release: n/a
Applies To: Engine
Component: I18N
Branches:
For now %LANGUAGES can only return the localized language name as specified in the po file by the translator.
However to properly select a language, it would be nice to have the language name in English as provided by the Locale::Language module.
configure
already makes use of it to display the list of languages enabled.
Besides there are some other problems in %LANGUAGES:
- it does not allow to specify an empty format, separator, or selector
- it does not make use of standard escape tokens
Here's the patch for trunk:
Index: lib/Foswiki/Macros/LANGUAGES.pm
===================================================================
--- lib/Foswiki/Macros/LANGUAGES.pm (revision 16498)
+++ lib/Foswiki/Macros/LANGUAGES.pm (working copy)
@@ -4,16 +4,26 @@
use strict;
use warnings;
+use Locale::Language;
+use Locale::Country;
+
sub LANGUAGES {
my ( $this, $params ) = @_;
- my $format = $params->{format} || " * \$langname";
- my $separator = $params->{separator} || "\n";
+
+ my $format = $params->{format};
+ $format = " * \$langname" unless defined $format;
+
+ my $separator = $params->{separator};
+ $separator = "\n" unless defined $separator;
$separator =~ s/\\n/\n/g;
+
my $selection = $params->{selection} || '';
$selection =~ s/\,/ /g;
$selection = " $selection ";
- my $marker = $params->{marker} || 'selected="selected"';
+ my $marker = $params->{marker};
+ $marker = 'selected="selected"' unless defined $marker;
+
# $languages is a hash reference:
my $languages = $this->i18n->enabled_languages();
@@ -21,21 +31,51 @@
my $result = '';
my $i = 0;
+ my @result = ();
foreach my $lang (@tags) {
my $item = $format;
my $name = ${$languages}{$lang};
$item =~ s/\$langname/$name/g;
$item =~ s/\$langtag/$lang/g;
+ $item =~ s/\$label/code2label($lang)/ge;
my $mark = ( $selection =~ / \Q$lang\E / ) ? $marker : '';
$item =~ s/\$marker/$mark/g;
- $result .= $separator if $i;
- $result .= $item;
+ push @result, $item;
$i++;
}
- return $result;
+ return Foswiki::expandStandardEscapes(join($separator, @result));
}
+sub code2label {
+ my $lang = shift;
+
+ my $label;
+ if ( $lang =~ /^(\w+)-(\w+)$/ ) {
+ my ( $lname, $cname ) = (
+ ( code2language( $1, LOCALE_CODE_ALPHA_2 ) || '' ),
+ ( code2country( $2, LOCALE_CODE_ALPHA_2 ) || '' )
+ );
+ if ( $lname && $cname ) {
+ $label = "$lname ($cname)";
+ }
+ elsif ($lname) {
+ $label = "$lname ($2)";
+ }
+ elsif ($cname) {
+ $label = "$1 ($cname)";
+ }
+ else {
+ $label = "$lang";
+ }
+ }
+ else {
+ $label = code2language( $lang, LOCALE_CODE_ALPHA_2 ) || "$lang";
+ }
+
+ return $label;
+}
+
A bit of docu:
--- data/System/VarLANGUAGES.txt (revision 16498)
+++ data/System/VarLANGUAGES.txt (working copy)
@@ -13,6 +13,7 @@
| =selection="%<nop>LANGUAGE%"= | Current language to be selected in list | =(none)= |
* =format= tokens:
| *Token* | *Meaning* |
+ | =$label= | language's name in English, automatically derived from the language's tag |
| =$langname= | language's name, as informed by the translators |
| =$langtag= | language's tag. Ex: =en=, =pt-br=, etc. |
* Example: =<select>%<nop>LANGUAGES{format="<option $marker value='$langtag'>$langname</option>" selection="%<nop>LANGUAGE%"}%</select>= creates an option list of the available languages with the current language selected
--
MichaelDaum - 31 Jan 2013
Definitely not for 1.1.7. No more RC's please... I announced 1.1.7 will be released tomorrow, but it will probably end up Saturday as I have other commitments. As far as changes to trunk, I suspect this probably needs a feature request.
SvenDowideit will be RM for 1.2.
--
GeorgeClark - 31 Jan 2013
Never mind. I see you marked it for the minor release. Still, adding macro format tokens probably needs a Feature?
--
GeorgeClark - 31 Jan 2013
Michael, Do you want to apply this to 1.2?
--
GeorgeClark - 23 Dec 2014
No, let's dump this patch.
--
MichaelDaum - 07 Jan 2015