The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Table::Trans - A simple translation system for templating

SYNOPSIS

    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.)

VERSION

This documents version 0.02 of Table-Trans corresponding to git commit 8c0335f2bc7f5d3202e5b327780915a9c5e74120 released on Thu Apr 22 15:22:34 2021 +0900.

DESCRIPTION

This module provides simple translation storage based on the Table::Readable format.

FUNCTIONS

add_trans

    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.

get_lang_name

    my $language = get_lang_name ($lang);

Given a language code like en, convert it into the native name of the language, like "English".

get_lang_trans

     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.

get_single_trans

    my $label = get_single_trans ($trans, $id, $lang);

Given $trans as read by "read_trans", get a single translation of $id for $lang.

read_trans

    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:

    my $trans = read_trans ($text, scalar => 1);

trans_to_json_file

    trans_to_json_file ('file.txt', 'file.json');

Convert the translations in file.txt into file.json.

write_trans

     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.

FORMAT

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

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: 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".

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.

DEPENDENCIES

JSON::Create

This is used for storing the translations in JSON format.

JSON::Parse

This is only used by the tests.

Table::Readable

This is used as the basic format for storing and editing translations.

BUGS

id is Indonesian

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.

SEE ALSO

CPAN

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.

GNU gettext

Data::Localize::Gettext

[⭐ Author: DMAKI; Date: 2019-10-04; Version: 0.00028]

File::Gettext

[⭐ Author: PJFL; Date: 2017-03-01; Version: v0.33.1]

Gettext

[Author: JBRIGGS; Date: 2000-01-09; Version: 0.01]

Locale::gettext

[⭐ Author: PVANDRY; Date: 2015-09-28; Version: 1.07]

Locale::Maketext::Gettext

[Author: IMACAT; Date: 2019-09-16; Version: 1.30]

Locale::XGettext

[⭐ Author: GUIDO; Date: 2018-11-04; Version: 0.7]

qbit::GetText

[Author: MADSKILL; Date: 2018-11-05; Version: 2.8]

Other formats and ideas

Lingua::String

[Author: NHORNE; Date: 2021-01-24; Version: 0.02]

Strings which come out in different languages depending on the environment.

Locale::Country::Multilingual

[⭐ Author: OSCHWALD; Date: 2014-08-22; Version: 0.25]

This does something like "get_lang_name" in this module, but more comprehensively.

MetaTrans

[Author: SKIM; Date: 2009-09-06; Version: 1.05]

"Class for creating multilingual meta-translators"

Pangloss

[⭐ Author: SPURKIS; Date: 2004-06-03; Version: 0.06]

"A multilingual web-based glossary." There's a huge amount of code here, but the last update was in 2004.

XML::TMX

[⭐ Author: AMBS; Date: 2017-09-07; Version: 0.36]

This module supports the TMX (Translation Memory eXchange) format, which is XML.

Other

OmegaT

OmegaT (link to Wikipedia) is a free-software translation system in Java which supports Gettext and TMX.

Trados

Trados (link to Wikipedia) is the most popular commercial translation memory software.

AUTHOR

Ben Bullock, <bkb@cpan.org>

COPYRIGHT & LICENCE

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.