1#
2#  Lang.pm
3#
4#  Language support.
5#
6#  Copyright (C) 1997, Cees de Groot
7#  Copyright (C) 2020, Agustin Martin
8# -------------------------------------------------------------------------
9
10package LinuxDocTools::Lang;
11
12use 5.006;
13use strict;
14
15use LinuxDocTools::Data::Strings_SBCS;
16use LinuxDocTools::Data::Strings_UTF8;
17use LinuxDocTools::Utils qw(ldt_log);
18use LinuxDocTools::Vars;
19
20use base qw(Exporter);
21
22# List all unconditionally exported symbols here.
23our @EXPORT = qw(Any2ISO ISO2Native ISO2English Xlat);
24
25# List all conditionally exported symbols here.
26# our @EXPORT_OK = qw();
27
28# Import :all to get everything.
29# our %EXPORT_TAGS = (all => [@EXPORT_OK]);
30
31# -------------------------------------------------------
32
33=head1 NAME
34
35LinuxDocTools::Lang - language name and translation functions
36
37=head1 SYNOPSIS
38
39  $isoname = Any2ISO ('deutsch');
40  $native  = ISO2Native ('de');
41  $engname = ISO2English ('nederlands');
42
43  $global->{language} = 'nl';
44  $dutch = Xlat ('Table of Contents');
45
46=head1 DESCRIPTION
47
48B<LinuxDocTools::Lang> gives a simple interface to various forms of language
49names, and provides a translation service. Languages can be specified in
50three different ways: by their native name, by their english name, and
51by their 2-letter ISO code. For example, you can specify the German
52language as C<deutsch>, as C<german> or as C<de>.
53
54=head1 FUNCTIONS
55
56=over 4
57
58=cut
59
60our @Languages = qw(
61  en english english
62  de deutsch german
63  nl nederlands dutch
64  fr fran�ais french
65  es espa�ol spanish
66  da dansk danish
67  no norsk norwegian
68  se svenska swedish
69  pt portuges portuguese
70  ca catal� catalan
71  it italiano italian
72  ro rom�n� romanian
73  ja japanese japanese
74  pl polski polish
75  ko korean korean
76  fi suomi finnish
77  zh_CN chinese-simplified chinese-simplified
78);
79
80
81=item Any2ISO
82
83Maps any of the three forms of languages to the ISO name. So either of
84these invocations:
85
86  Any2ISO ('dutch');
87  Any2ISO ('nederlands');
88  Any2ISO ('nl');
89
90will return the string C<"nl">.
91
92=cut
93
94sub Any2ISO
95{
96  my $lang = shift (@_);
97
98  my $i = 0;
99  foreach my $l (@Languages)
100    {
101      ($l eq $lang) && last;
102      $i++;
103    }
104  return $Languages[(int $i / 3) * 3];
105}
106
107
108=item ISO2Native
109
110Maps the ISO code to the native name of the language.
111
112=cut
113
114sub ISO2Native
115{
116  my $iso = shift (@_);
117
118  my $i = 0;
119  foreach my $l (@Languages)
120    {
121      ($l eq $iso) && last;
122      $i++;
123    }
124  return $Languages[$i + 1];
125
126}
127
128
129=item ISO2English
130
131Maps the ISO code to the english name of the language.
132
133=cut
134
135sub ISO2English
136{
137  my $iso = shift (@_);
138
139  my $i = 0;
140  foreach my $l (@Languages)
141    {
142      ($l eq $iso) && last;
143      $i++;
144    }
145  return $Languages[$i + 2];
146}
147
148=item Xlat
149
150Translates its (English) argument to the language specified by the
151current value of C<$gobal-E<gt>{language}>. The module, in its source
152file, contains a data structure, indexed by the English strings, that
153has all available translations.
154
155=cut
156
157sub Xlat {
158  my ($txt) = @_;
159  my $string = $txt;
160  my $translations = ( $global->{charset} eq "utf-8" )
161    ? $LinuxDocTools::Data::Strings_UTF8::translations
162    : $LinuxDocTools::Data::Strings_SBCS::translations;
163
164  if ( defined $global->{language}
165       && defined $translations->{$txt}{$global->{language}}
166       && $global->{language} ne "en" ){
167    $string = $translations->{$txt}{$global->{language}};
168  }
169  ldt_log "LinuxDocTools::Lang.pm::Xlang: in_string: \"$txt\", language: \"$global->{language}\", charset: \"$global->{charset}\", out_string: \"$string\"";
170
171  return $string;
172};
173
1741;
175
176=back
177
178=head1 AUTHOR
179
180Cees de Groot, C<E<lt>cg@pobox.comE<gt>>
181
182=cut
183
1841;
185