From 87b0de4c742e6032bcb6bf8750b1d8144e1a9860 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 6 Sep 2012 10:00:17 +0200 Subject: [PATCH] =?utf8?q?SL::Locale::String-Klasse=20f=C3=BCr=20verz?= =?utf8?q?=C3=B6gerte=20=C3=9Cbersetzung=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/Helper/Sorted.pm | 2 +- SL/Locale.pm | 2 + SL/Locale/String.pm | 120 +++++++++++++++++++++++++++++++++ scripts/locales.pl | 2 +- 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 SL/Locale/String.pm diff --git a/SL/Controller/Helper/Sorted.pm b/SL/Controller/Helper/Sorted.pm index 8699f00c7..c9e577911 100644 --- a/SL/Controller/Helper/Sorted.pm +++ b/SL/Controller/Helper/Sorted.pm @@ -22,7 +22,7 @@ sub make_sorted { while (my ($column, $spec) = each %specs) { next if $column =~ m/^[A-Z_]+$/; - $spec = $specs{$column} = { title => $spec } if !ref $spec; + $spec = $specs{$column} = { title => $spec } if (ref($spec) || '') ne 'HASH'; $spec->{model} ||= $specs{MODEL}; $spec->{model_column} ||= $column; diff --git a/SL/Locale.pm b/SL/Locale.pm index 5016900fe..8fa37805d 100644 --- a/SL/Locale.pm +++ b/SL/Locale.pm @@ -214,6 +214,8 @@ sub text { my $self = shift; my $text = shift; + return $text->translated if (ref($text) || '') eq 'SL::Locale::String'; + if ($self->{texts}->{$text}) { $text = $self->{iconv}->convert($self->{texts}->{$text}); } else { diff --git a/SL/Locale/String.pm b/SL/Locale/String.pm new file mode 100644 index 000000000..42ceb22a6 --- /dev/null +++ b/SL/Locale/String.pm @@ -0,0 +1,120 @@ +package SL::Locale::String; + +use strict; + +use parent qw(Rose::Object Exporter); + +use Rose::Object::MakeMethods::Generic ( + scalar => [ qw(untranslated) ], +); + +our @EXPORT = qw(t8); + +use overload '""' => \&translated; + +sub translated { + my ($self) = @_; + return $::locale ? $::locale->text($self->untranslated) : $self->untranslated; +} + +sub t8 { + my $string = $_[ ref($_[0]) || ($_[0] eq 'SL::Locale::String') ? 1 : 0 ]; + return SL::Locale::String->new(untranslated => $string); +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::Locale::String - Helper class for translating strings at a later +date (e.g. use at compile time, actual translation during execution +time) + +=head1 SYNOPSIS + + use SL::Locale::String; + + use SL::Controller::Helper::Sorted; + + __PACKAGE__->make_sorted( + ... + qty => { title => t8("Quantity") }, + ); + +=head1 OVERVIEW + +Every string that should be translated must be recognized by our +translation helper script C