File Coverage

blib/lib/WE/Util/LangString.pm
Criterion Covered Total %
statement 30 40 75.0
branch 11 18 61.1
condition 3 3 100.0
subroutine 8 9 88.8
pod 6 6 100.0
total 58 76 76.3


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: LangString.pm,v 1.8 2004/04/05 20:33:05 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8             # Copyright (C) 2002 Slaven Rezic.
9             # This is free software; you can redistribute it and/or modify it under the
10             # terms of the GNU General Public License, see the file COPYING.
11              
12             #
13             # Mail: slaven@rezic.de
14             # WWW: http://we-framework.sourceforge.net
15             #
16              
17             package WE::Util::LangString;
18              
19 18     18   2086 use base 'Exporter';
  18         37  
  18         2096  
20              
21 18     18   119 use strict;
  18         39  
  18         656  
22 18     18   92 use vars qw($VERSION @EXPORT_OK $DEFAULT_LANG);
  18         35  
  18         12723  
23             $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/);
24              
25             @EXPORT_OK = qw(new_langstring langstring set_langstring);
26              
27             $DEFAULT_LANG = 'en' unless defined $DEFAULT_LANG;
28              
29             =head1 NAME
30              
31             WE::Util::LangString - language-dependent strings
32              
33             =head1 SYNOPSIS
34              
35             use WE::Util::LangString qw(new_langstring langstring set_langstring);
36              
37             =head1 DESCRIPTION
38              
39             This module deals with language-dependent strings.
40              
41             =head1 METHODS
42              
43             =over 4
44              
45             =item WE::Util::LangString->new(en => "english title", de => "german title")
46              
47             Create a new C object and optionally initializes
48             the object with values.
49              
50             =cut
51              
52             sub new {
53 6     6 1 16 my($class, %args) = @_;
54 6         17 my $self = {%args};
55 6         32 bless $self, $class;
56             }
57              
58             =item new_langstring(en => "english title", de => "german title")
59              
60             Same as the C constructor, but shorter form.
61              
62             =cut
63              
64 2     2 1 16 sub new_langstring { WE::Util::LangString->new(@_) }
65              
66             =item $obj->get([$language])
67              
68             Get the value for the specified language. If no language is specified
69             or there is no language value in the object, then the english version
70             is returned. If there is no english version, return the first value
71             found in the object.
72              
73             =item $obj->langstring([$language])
74              
75             =item langstring($string, [$language])
76              
77             This is an alias for C.
78              
79             =cut
80              
81             sub get {
82 19     19 1 193 my($self, $language) = @_;
83 19 100       73 if (UNIVERSAL::isa($self, __PACKAGE__)) {
84 15 50 100     123 defined $language && exists $self->{$language}
    100          
85             ? $self->{$language} # use language string asked for...
86             : exists $self->{'en'}
87             ? $self->{'en'} # use english fallback
88             : $self->{(keys %$self)[0]}; # use first one
89             } else {
90             # treat $self as a string
91 4         17 $self;
92             }
93             }
94              
95             *langstring = \&get;
96              
97             =item set_langstring($obj,$language,$string,[$default_language])
98              
99             Set the string C<$string> for language C<$language> to the object
100             C<$obj>. If C<$obj> is not yet a C object, then
101             it will be blessed automatically into it. If C<$language> is not
102             specified, then a default language (as set in C<$DEFAULT_LANG>,
103             normally english) is used.
104              
105             =cut
106              
107             sub set_langstring {
108 5     5 1 17 my($lang, $string, $default_lang) = @_[1..3];
109 5 100       27 if (!UNIVERSAL::isa($_[0], __PACKAGE__)) {
110 3 100       9 $default_lang = $DEFAULT_LANG unless defined $default_lang;
111 3         9 $_[0] = __PACKAGE__->new($default_lang => $_[0]);
112             }
113 5         12 $_[0]->{$lang} = $string;
114 5         11 $_[0];
115             }
116              
117             =item dump
118              
119             Dump the content of the langstring as a one-line string.
120              
121             =cut
122              
123             sub dump {
124 2     2 1 5 my $self = shift;
125 2         3 my @s;
126 2         12 foreach my $lang (sort keys %$self) {
127 4         7 my $val = $self->{$lang};
128 4 100       8 $val = "(undef)" if !defined $val;
129 4         12 push @s, "$lang: $val";
130             }
131 2         28 join(", ", @s);
132             }
133              
134             =item concat($oldstr, $newstr)
135              
136             Add C<$newstr> to C<$oldstr>. If C<$oldstr> is a
137             C object, than add C<$newstr> to all language
138             variants in the object. If both arguments are C
139             objects, then the corresponding language versions are concatenated.
140              
141             =cut
142              
143             sub concat ($$) {
144 0 0   0 1   if (UNIVERSAL::isa($_[0], __PACKAGE__)) {
145 0 0         if (UNIVERSAL::isa($_[1], __PACKAGE__)) {
146 0           while(my($k,$v) = each %{ $_[1] }) {
  0            
147 0 0         if (exists $_[0]->{$k}) {
148 0           $_[0]->{$k} .= $v;
149             }
150             }
151             } else {
152 0           while(my($k) = each %{ $_[0] }) {
  0            
153 0           $_[0]->{$k} .= $_[1];
154             }
155             }
156             } else {
157 0           $_[0] .= $_[1];
158             }
159             }
160              
161             1;
162              
163             __END__