aboutsummaryrefslogtreecommitdiffstats
path: root/Perl/Webwml/Langs.pm
blob: 16857146ff20e7a6ef7aab1a81d03e39dfca3670 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/perl

##  Copyright (C) 2001  Denis Barbier <barbier@debian.org>
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.

=head1 NAME

Webwml::Langs - get all languages in which w.d.o is translated

=head1 SYNOPSIS

 use Webwml::Langs;
 my $l = Webwml::Langs->new();
 my @abbr  = $l->iso3166();
 my @langs = $l->names();
 my %lang1 = $l->iso_name();
 my %lang2 = $l->name_iso();

=head1 DESCRIPTION

This module parses english/template/debian/languages.wml and returns
the list of languages in which Debian web site is translated.

=head1 METHODS

=over 4

=cut

package Webwml::Langs;

use Carp;
use Local::VCS;

use strict;
use warnings;

=item new

This is the constructor.  If called with an argument, it tells where to
find top-level webwml directory.  Otherwise it is automatically
determined from VCS meta-info.

   my $l = Webwml::Langs->new("/path/to/webwml");

=cut

sub new {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $VCS = Local::VCS->new();
	my $root;
	if (@_)
	{
		$root = shift;
	} else
	{
		$root = $VCS->get_topdir();
	}
	my $self = _read($root);
	bless ($self, $class);
	return $self;
}


# read the languages.wml, search for the %langs variable, and evaluate it
# within this program
# TODO: pretty hacky, rewrite to avoid eval()
# TODO: or rather define the lnaguages in a module, and include those in
#       languages.wml
sub _read {
	my $root = shift || Local::VCS->get_topdir();

	open( my $file, '<', "$root/english/template/debian/languages.wml")
		or croak ("Unable to read $root/english/template/debian/languages.wml");

	my $decl = '';
	my $start = 1;

	#   This variable is declared in english/template/debian/languages.wml
	my %langs;
	while ( my $line = <$file> )
	{
		# continue until we find the line defining the %langs variable
		next  if ( $start  and  not $line =~ m{my \s+ \%langs \s* =}x );
		# skip comments
		next if $line =~ m{^\s*#};

		$start = 0;
		$decl .= $line;

		# end of the variable declaration
		last if $line =~ m{\);};
	}
	close( $file );

	# remove "my" from the front of the declaration and evaluate it
	$decl =~ s/^\s*my//s;
	eval "$decl";

	carp $@ . " when parsing \n$decl\nin Webwml::Langs\n" if $@;

	return \%langs;
}

=item iso3166

Return the list of country codes.

   my @abbr = $l->iso3166();

=cut

sub iso3166
{
	my $self = shift;
	return values %{$self};
}

=item names

Return the list of language names.

   my @langs = $l->names();

=cut

sub names
{
	my $self = shift;
	return keys %{$self};
}

=item iso_name

Return a hash array I<code>/I<name>.

   my %lang1 = $l->iso_name();

=cut

sub iso_name
{
	my $self = shift;
	return map { ${$self}{$_} => $_ } keys %$self;
}

=item name_iso

Return a hash array I<name>/I<code>.

   my %lang2 = $l->name_iso();

=cut

sub name_iso
{
	my $self = shift;
	return %{$self};
}

=back

=head1 AUTHOR

Copyright (C) 2001  Denis Barbier <barbier@debian.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

=cut

1;

© 2014-2024 Faster IT GmbH | imprint | privacy policy