| 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 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 18 | ▶ | use vars qw($VERSION @EXPORT_OK $DEFAULT_LANG); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | 18 | $VERSION = sprintf("%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 18 | @EXPORT_OK = qw(new_langstring langstring set_langstring); | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | $DEFAULT_LANG = 'en' unless defined $DEFAULT_LANG; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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<WE::Util::LangString> object and optionally initializes | ||||||||||||||||||||||||||||||||
| 48 | the object with values. | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 6 | my($class, %args) = @_; | |||||||||||||||||||||||||||||||
| 54 | 6 | my $self = {%args}; | |||||||||||||||||||||||||||||||
| 55 | 6 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | =item new_langstring(en => "english title", de => "german title") | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | Same as the C<new> constructor, but shorter form. | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | sub new_langstring { WE::Util::LangString->new(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
new_langstring: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 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<get>. | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 19 | ▶ | sub get { | ||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 82 | 19 | my($self, $language) = @_; | |||||||||||||||||||||||||||||||
| 83 | 19 | ▶ | if (UNIVERSAL::isa($self, __PACKAGE__)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | defined $language && exists $self->{$language} | ||||||||||||||||||||||||||||||||
| 85 | ? $self->{$language} # use language string asked for... | ||||||||||||||||||||||||||||||||
| 86 | : exists $self->{'en'} | ||||||||||||||||||||||||||||||||
| 87 | ? $self->{'en'} # use english fallback | ||||||||||||||||||||||||||||||||
| 88 | 15 | ▶ | : $self->{(keys %$self)[0]}; # use first one | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $language and exists $self->{$language}
MC/DC100%2/2
defined $language and exists $self->{$language}
defined $language
exists $self->{$language}
Truth table100%3/3
A and B
A= defined $languageB= exists $self->{$language}
| |||||||||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||||||
| 90 | # treat $self as a string | ||||||||||||||||||||||||||||||||
| 91 | 4 | $self; | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 18 | *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<WE::Util::LangString> 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 | 5 | ▶ | sub set_langstring { | ||||||||||||||||||||||||||||||
|
Subroutine
set_langstring: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 108 | 5 | my($lang, $string, $default_lang) = @_[1..3]; | |||||||||||||||||||||||||||||||
| 109 | 5 | ▶ | if (!UNIVERSAL::isa($_[0], __PACKAGE__)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | $default_lang = $DEFAULT_LANG unless defined $default_lang; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 3 | $_[0] = __PACKAGE__->new($default_lang => $_[0]); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | 5 | $_[0]->{$lang} = $string; | |||||||||||||||||||||||||||||||
| 114 | 5 | $_[0]; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =item dump | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | Dump the content of the langstring as a one-line string. | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 2 | ▶ | sub dump { | ||||||||||||||||||||||||||||||
|
Subroutine
dump: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 124 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 125 | 2 | my @s; | |||||||||||||||||||||||||||||||
| 126 | 2 | foreach my $lang (sort keys %$self) { | |||||||||||||||||||||||||||||||
| 127 | 4 | my $val = $self->{$lang}; | |||||||||||||||||||||||||||||||
| 128 | 4 | ▶ | $val = "(undef)" if !defined $val; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 129 | 4 | push @s, "$lang: $val"; | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | 2 | join(", ", @s); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =item concat($oldstr, $newstr) | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | Add C<$newstr> to C<$oldstr>. If C<$oldstr> is a | ||||||||||||||||||||||||||||||||
| 137 | C<WE::Util::LangString> object, than add C<$newstr> to all language | ||||||||||||||||||||||||||||||||
| 138 | variants in the object. If both arguments are C<WE::Util::LangString> | ||||||||||||||||||||||||||||||||
| 139 | objects, then the corresponding language versions are concatenated. | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub concat ($$) { | ||||||||||||||||||||||||||||||
|
Subroutine
concat: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (UNIVERSAL::isa($_[0], __PACKAGE__)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (UNIVERSAL::isa($_[1], __PACKAGE__)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | while(my($k,$v) = each %{ $_[1] }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (exists $_[0]->{$k}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 0 | $_[0]->{$k} .= $v; | |||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | } else { | ||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | while(my($k) = each %{ $_[0] }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 0 | $_[0]->{$k} .= $_[1]; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | } else { | ||||||||||||||||||||||||||||||||
| 157 | 0 | $_[0] .= $_[1]; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 18 | 1; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||||||