summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-08-14 13:05:59 +0200
committerYorhel <git@yorhel.nl>2009-08-17 16:27:14 +0200
commit4b918796f8d85d40d55dbf2674a2b691d802ff02 (patch)
treed8247ab322ef03d5ff072a2c329f7e2cb0cd0461
parentdc9ff373251c7a0f543216173939a8bf02f519be (diff)
Started on a framework for interface I18N
You can ignore the entries in lang.txt, those are simply for testing purposes.
-rw-r--r--data/lang.txt54
-rw-r--r--lib/VNDB/Func.pm9
-rw-r--r--lib/VNDB/L10N.pm86
-rwxr-xr-xutil/vndb.pl9
4 files changed, 157 insertions, 1 deletions
diff --git a/data/lang.txt b/data/lang.txt
new file mode 100644
index 00000000..cd6cd010
--- /dev/null
+++ b/data/lang.txt
@@ -0,0 +1,54 @@
+This file contains all the translatable text of the VNDB interface.
+
+IMPORTANT: This file is encoded in UTF-8 and uses UNIX-style line endings,
+ make sure your text editor is configured to correctly handle this!
+
+The syntax of this file is as follows: empty lines, or lines beginning with a
+'#' are ignored by the parser. Each translatable piece of text is identified by
+a 'key', this is usually the English text, but can also be a short identifier
+starting with an underscore. A key line starts with a colon ':' and is directly
+followed by the key itself. The lines following such a key line specify which
+text to use for each language. A language text starts with the language tag,
+followed by a space or asterisk, followed by a colon, another space, and the
+text for that language. Here's an example of a line:
+
+ # this is a comment
+ :Age rating
+ en : Age rating
+ nl*: Minimum leeftijd
+
+In this example, "Age rating" is the 'key', and the lines following are the
+translations. If a line does not have a translation for a particular language,
+the system will fall back to the English version (i.e. the string that starts
+with 'en:'). If there is no English version, either, it will use the key
+itself. The key and the English translation are the same in the above example,
+so in that case the translation can be left out entirely:
+
+ # this has the same effect as the previous example
+ :Age rating
+ nl*: Minimum leeftijd
+
+Because VNDB is in constant development, changes to the interface text are
+inevitable, and the translations will get out of sync with the English text
+very quickly. To indicate which lines should be synchronised to their English
+counterparts, and asterisk is used between the language tag and the colon, as
+used in the above examples. As soon as a line is retranslated or synchronised
+again by the translator, (s)he should replace the asterick with a space to
+indicate that it has been checked.
+
+(Actual processing of this file begins after the following line)
+/intro
+
+
+:test
+en : (en test)
+ru : (ru test)
+
+:multiline
+en : en multi
+ en line
+
+ru : ru multi
+ ru line
+
+
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm
index 4ca20dc5..31a04f8a 100644
--- a/lib/VNDB/Func.pm
+++ b/lib/VNDB/Func.pm
@@ -6,7 +6,7 @@ use warnings;
use YAWF ':html';
use Exporter 'import';
use POSIX 'strftime', 'ceil', 'floor';
-our @EXPORT = qw| shorten age date datestr monthstr userstr bb2html gtintype liststat clearfloat cssicon tagscore|;
+our @EXPORT = qw| shorten age date datestr monthstr userstr bb2html gtintype liststat clearfloat cssicon tagscore mt |;
# I would've done this as a #define if this was C...
@@ -267,5 +267,12 @@ sub tagscore {
}
+# short wrapper around maketext()
+# (not thread-safe, in the same sense as YAWF::XML. But who cares about threads, anyway?)
+sub mt {
+ return $YAWF::OBJ->{l10n}->maketext(@_);
+}
+
+
1;
diff --git a/lib/VNDB/L10N.pm b/lib/VNDB/L10N.pm
new file mode 100644
index 00000000..cbf6a239
--- /dev/null
+++ b/lib/VNDB/L10N.pm
@@ -0,0 +1,86 @@
+
+use strict;
+use warnings;
+
+{
+ package VNDB::L10N;
+ use base 'Locale::Maketext';
+
+ sub fallback_languages { ('en') };
+
+ # can be called as either a subroutine or a method
+ sub loadfile {
+ my %lang = (
+ en => \%VNDB::L10N::en::Lexicon,
+ ru => \%VNDB::L10N::ru::Lexicon,
+ );
+
+ open my $F, '<:utf8', $VNDB::ROOT.'/data/lang.txt' or die "Opening language file: $!\n";
+ my($empty, $line, $key, $lang) = (0, 0);
+ while(<$F>) {
+ chomp;
+ $line++;
+
+ # ignore intro
+ if(!defined $key) {
+ $key = 0 if /^\/intro$/;
+ next;
+ }
+ # ignore comments
+ next if /^#/;
+ # key
+ if(/^:(.+)$/) {
+ $key = $1;
+ $lang = undef;
+ $empty = 0;
+ next;
+ }
+ # locale string
+ if(/^([a-z_-]{2,7})[ *]: (.+)$/) {
+ $lang = $1;
+ die "Unknown language on #$line: $lang\n" if !$lang{$lang};
+ die "Unknown key for locale on #$line\n" if !$key;
+ $lang{$lang}{$key} = $2;
+ $empty = 0;
+ next;
+ }
+ # multi-line locale string
+ if($lang && /^\s+([^\s].*)$/) {
+ $lang{$lang}{$key} .= ''.("\n"x$empty)."\n$1";
+ $empty = 0;
+ next;
+ }
+ # empty string (count them in case they're part of a multi-line locale string)
+ if(/^\s*$/) {
+ $empty++;
+ next;
+ }
+ # something we didn't expect
+ die "Don't know what to do with line $line\n";
+ }
+ close $F;
+
+ # dev.
+ use Data::Dumper 'Dumper';
+ warn Dumper \%lang;
+ }
+}
+
+
+{
+ package VNDB::L10N::en;
+ use base 'VNDB::L10N';
+ our %Lexicon = (
+ _AUTO => 1
+ );
+}
+
+{
+ package VNDB::L10N::ru;
+ use base 'VNDB::L10N::en';
+ our %Lexicon;
+}
+
+
+1;
+
diff --git a/util/vndb.pl b/util/vndb.pl
index 455a1c23..7dd9b6fa 100755
--- a/util/vndb.pl
+++ b/util/vndb.pl
@@ -17,6 +17,7 @@ use lib $ROOT.'/lib';
use YAWF ':html';
+use VNDB::L10N;
our(%O, %S);
@@ -31,6 +32,10 @@ $S{skins} = readskins();
require $ROOT.'/data/global.pl';
+# load lang.dat
+VNDB::L10N::loadfile();
+
+
YAWF::init(
%O,
namespace => 'VNDB',
@@ -42,6 +47,10 @@ YAWF::init(
sub reqinit {
my $self = shift;
+
+ # TODO: get language from user setting (cookie, query string, whatever)
+ $self->{l10n} = VNDB::L10N->get_handle();
+
$self->authInit;
# check for IE6