Table::Trans - A simple translation system for templating
use utf8; use Table::Trans ':all'; my $text =<<EOF; id: insect ja: 昆虫 de: Insekten id: frog ja: 蛙 de: Froschlurche EOF my $trans = read_trans ($text, scalar => 1); print $trans->{frog}{ja}, "\n"; my %vars; get_lang_trans ($trans, \%vars, 'ja'); print $vars{trans}{frog}, "\n";
produces output
蛙 蛙
(This example is included as synopsis.pl in the distribution.)
This documents version 0.02 of Table-Trans corresponding to git commit 8c0335f2bc7f5d3202e5b327780915a9c5e74120 released on Thu Apr 22 15:22:34 2021 +0900.
This module provides simple translation storage based on the Table::Readable format.
add_trans ($trans, "$Bin/lang/translations.txt");
Add translations to $trans from another file. This is like "read_trans" but it enables you to use more than one file of translations. If there are two translations with the same id value, it prints a warning.
$trans
id
my $language = get_lang_name ($lang);
Given a language code like en, convert it into the native name of the language, like "English".
en
get_lang_trans ($trans, $vars, $lang);
Put the translations from $trans for language $lang into $vars->{trans}. This is intended for the case where you want to run a template file several times with different languages. For example using Template you might have
[ % trans.frog % ]
in your template, then run it with
for my $lang (qw!en ja de!) { get_lang_trans ($trans, \%vars, $lang); $vars{lang} = $lang; $tt->process ('in.tmpl', \%vars, "out.$lang.html", binmode => 'utf8'); }
to produce similar outputs in three languages.
my $label = get_single_trans ($trans, $id, $lang);
Given $trans as read by "read_trans", get a single translation of $id for $lang.
$id
$lang
my $trans = read_trans ('file.txt');
Also get the order:
my ($trans, $order) = read_trans ('file.txt');
Read a file of translations in Table::Readable format.
The same options as "read_table" in Table::Readable are accepted. To read from a scalar instead of a file, use scalar => 1:
scalar => 1
my $trans = read_trans ($text, scalar => 1);
trans_to_json_file ('file.txt', 'file.json');
Convert the translations in file.txt into file.json.
write_trans (\%trans, [qw/en ja es/], "output.txt", \@id_order);
Write the translations stored in %trans for the languages "en", "ja" and "es" in the order given by @id_order to the file output.txt in the Table::Readable format.
%trans
@id_order
output.txt
The basic format of the translations is the Table::Readable format, with each translated piece of text being identified with the code id then each particular language having its own entry.
Macros to insert another translation can be used in the form {{id}} and the translation of id in the language of the entry will be inserted, so if we have
{{id}}
id: ape en: monkey ja: さる id: monkeybusiness en: {{ape}} business ja: {{ape}}さわぎ
then the ja (Japanese) entry for the ID "monkeybusiness" will be さ るさわぎ and the English entry will be "monkey business".
ja
To use the same substitution for every language, use the key "all":
id: email all: bkb@cpan.org id: contact en: Email me at <a href='mailto:{{email}}'>{{email}}</a> ja: メルアド:<a href='mailto:{{email}}'>{{email}}</a>
If either all or the specific language translation (en or ja) does not exist, a fatal error occurs.
all
This is used for storing the translations in JSON format.
This is only used by the tests.
This is used as the basic format for storing and editing translations.
Unfortunately the two-letter ISO code for the Indonesian language is id, so at some point it will become necessary to change to a different key. I haven't done it yet due to the work involved in changing a large number of files over to a different key, and because I am not currently supporting Indonesian language anywhere.
There are several modules on CPAN for dealing with the GNU gettext format, and one for dealing with the TMX format for human-language translations.
[⭐ Author: DMAKI; Date: 2019-10-04; Version: 0.00028]
2019-10-04
0.00028
[⭐ Author: PJFL; Date: 2017-03-01; Version: v0.33.1]
2017-03-01
v0.33.1
[Author: JBRIGGS; Date: 2000-01-09; Version: 0.01]
2000-01-09
0.01
[⭐ Author: PVANDRY; Date: 2015-09-28; Version: 1.07]
2015-09-28
1.07
[Author: IMACAT; Date: 2019-09-16; Version: 1.30]
2019-09-16
1.30
[⭐ Author: GUIDO; Date: 2018-11-04; Version: 0.7]
2018-11-04
0.7
[Author: MADSKILL; Date: 2018-11-05; Version: 2.8]
2018-11-05
2.8
[Author: NHORNE; Date: 2021-01-24; Version: 0.02]
2021-01-24
0.02
Strings which come out in different languages depending on the environment.
[⭐ Author: OSCHWALD; Date: 2014-08-22; Version: 0.25]
2014-08-22
0.25
This does something like "get_lang_name" in this module, but more comprehensively.
[Author: SKIM; Date: 2009-09-06; Version: 1.05]
2009-09-06
1.05
"Class for creating multilingual meta-translators"
[⭐ Author: SPURKIS; Date: 2004-06-03; Version: 0.06]
2004-06-03
0.06
"A multilingual web-based glossary." There's a huge amount of code here, but the last update was in 2004.
[⭐ Author: AMBS; Date: 2017-09-07; Version: 0.36]
2017-09-07
0.36
This module supports the TMX (Translation Memory eXchange) format, which is XML.
OmegaT (link to Wikipedia) is a free-software translation system in Java which supports Gettext and TMX.
Trados (link to Wikipedia) is the most popular commercial translation memory software.
Ben Bullock, <bkb@cpan.org>
This package and associated files are copyright (C) 2008-2021 Ben Bullock.
You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.
To install Table::Trans, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Table::Trans
CPAN shell
perl -MCPAN -e shell install Table::Trans
For more information on module installation, please visit the detailed CPAN module installation guide.