1 package SL::Locale::String;
5 use parent qw(Rose::Object Exporter);
7 use Rose::Object::MakeMethods::Generic (
8 scalar => [ qw(untranslated) ],
14 use overload '""' => \&translated;
18 return $::locale ? $::locale->text($self->untranslated, $self->args) : $self->untranslated;
22 shift if $_[0] eq __PACKAGE__;
25 return SL::Locale::String->new(untranslated => $string, args => \@_);
37 SL::Locale::String - Helper class for translating strings at a later
38 date (e.g. use at compile time, actual translation during execution
43 use SL::Locale::String;
45 use SL::Controller::Helper::Sorted;
47 __PACKAGE__->make_sorted(
49 qty => { title => t8("Quantity") },
54 Every string that should be translated must be recognized by our
55 translation helper script C<script/locales.pl> somehow. It recognizes
56 certain function calls as translation instructions and extracts its
57 arguments for translation by developers/translators.
59 This works well for calls that occur during execution time: C<<
60 $::locale->text("Untranslated") >>. However, for untranslated strings
61 that need to be used at compile time this fails in subtle and not so
62 subtle ways. If it happens in a module that is C<use>d directly from
63 the dispatcher then C<$::locale> is not defined and such a call would
64 end in an error. For modules like controllers that are C<require>d
65 during execution time it seems to work, but in FastCGI situations this
66 means that the first call determines the language and all subsequent
67 calls end up using the same language no matter which language the user
70 This class solves the issue by providing a small function called L<t8>
71 which can be used instead of C<< $::locale->text() >>. It is
72 recognized by C<script/locales.pl>. The untranslated string given to
73 L<t8> is stored in an instance of C<SL::Locale::String> and translated
74 only when requested either by calling L<translated> or by
77 Instances of this class can safely be handed over to C<<
78 $::locale->text() >> which knows how to handle them (and not to
81 The function L<t8> is exported by default.
85 =head2 EXPORTED FUNCTIONS
89 =item C<t8 $untranslated_string>
91 Returns a new instance of C<SL::Locale::String> and sets its
92 L<untranslated> member to C<$untranslated_string>. This function is
93 exported and cannot be called as a class or instance method.
97 =head2 INSTANCE FUNCTIONS
101 =item C<untranslated [$new_untranslated]>
103 Gets or sets the untranslated string.
107 Returns the translated version of the untranslated string. Translation
108 occurs when this function is called and not when the object instance
111 This function is also called during stringification.
121 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>