1 |
package Bibulus::Lang; |
package Bibulus::Lang; |
2 |
|
|
|
use 5.008; |
|
|
use strict; |
|
3 |
use warnings; |
use warnings; |
4 |
use Carp; |
use Carp; |
5 |
|
|
6 |
use Bibulus::Lang::da; |
# Accepted languages: |
7 |
use Bibulus::Lang::en; |
# [We don't want to put them into separate directories, |
8 |
|
# so we just require them here. It seems to work...] |
9 |
|
require 'da.pm'; |
10 |
|
require 'en.pm'; |
11 |
|
# (Remember to add them as well in the 'lang' function!) |
12 |
|
|
13 |
sub new { |
sub new { |
14 |
my $class = shift; |
my $class = shift; |
24 |
my $self = shift; |
my $self = shift; |
25 |
if (@_) { |
if (@_) { |
26 |
my $l = shift; |
my $l = shift; |
27 |
$self->{LANG} = $l; |
if ($l =~ /^(en|da)$/) { # accepted languages |
28 |
if ($l eq 'en') { |
$self->{LANG} = $l; |
|
our @ISA = qw(Bibulus::Lang::en); |
|
|
} elsif ($l eq 'da') { |
|
|
our @ISA = qw(Bibulus::Lang::da); |
|
29 |
} else { |
} else { |
30 |
warn "Language $l unknown.\n"; |
warn "Language $l unknown.\n"; |
31 |
} |
} |
33 |
return $self->{LANG}; |
return $self->{LANG}; |
34 |
} |
} |
35 |
|
|
36 |
|
# This is magic: Whenever a function in Bibulus::Lang is called |
37 |
|
# that doesn't exist, this function will search for it in the |
38 |
|
# relevant language module. |
39 |
|
# E.g., if Bibulus::Lang::mastersthesis is called and |
40 |
|
# $self->{LANG} eq 'da', then Bibulus::Lang::da:mastersthesis |
41 |
|
# will be called instead (and that will presumably return 'speciale'). |
42 |
|
sub AUTOLOAD { |
43 |
|
my ($self) = @_; |
44 |
|
my $f = $AUTOLOAD; |
45 |
|
$f =~ /DESTROY/ and return; # to avoid error message |
46 |
|
$f =~ s/(.*::)/$1$self->{LANG}\:\:/; |
47 |
|
&$f(@_); |
48 |
|
} |
49 |
|
|
50 |
1; |
1; |
51 |
__END__ |
__END__ |
52 |
|
|
71 |
print $l->test, "\n"; |
print $l->test, "\n"; |
72 |
my $l2 = new Bibulus::Lang; |
my $l2 = new Bibulus::Lang; |
73 |
print $l2->test, "\n"; |
print $l2->test, "\n"; |
74 |
|
print $l->test, "\n"; |
75 |
|
|
76 |
|
|
77 |
All languages should provide the following functions: |
All languages should provide the following functions: |