| SweetPea-Application documentation | Contained in the SweetPea-Application distribution. |
SweetPea::Application::Locale - Localization handling for SweetPea-Application.
The new method instantiates a new SweetPea::Application::Locale object
which use Config::Any w/ YAML::Syck as a base class to provide access
to the yml locales.
$s->plug( 'config', sub { return SweetPea::Application::Locale->new($s); });
The language method selects the specific localization data(yml) file to be
used to provide translations to the application.
$s->locale->language('/en');
The text method retrieves the translation text from the locale file selected
with the "language" method. Defaults to "en" (english) translation.
Al Newkirk, <al.newkirk at awnstudio.com>
| SweetPea-Application documentation | Contained in the SweetPea-Application distribution. |
package SweetPea::Application::Locale; use warnings; use strict; use base 'Config::Any'; use YAML::Syck; use File::Find;
sub new { my ($class, $s) = @_; my $self = {}; my $keys = {}; my @files = (); my $path = $s->path('/sweet/locales'); bless $self, $class; if (-e $path) { find( sub{ if ($File::Find::name =~ /\.yml$/) { my $id = $File::Find::name ; push @files, $File::Find::name ; $id =~ s/(^$path|\.yml$)//; $id =~ s/\.yml$//; $keys->{$id} = $File::Find::name ; } }, $path); $self->{data} = $self->load_files( { files => \@files, use_ext => 1, flatten_to_hash => 1 } ); } $self->{keys} = $keys; $self->{base} = $s; $self->{lang} = ''; return $self; }
sub language { my ($self, $key) = @_; if ( $key ) { if (defined $self->{keys}->{$key}) { $self->{lang} = $self->{data}->{$self->{keys}->{$key}} if defined $self->{data}->{$self->{keys}->{$key}}; return 1; } } return undef; }
sub text { my ($self, $key, @text) = @_; if ($self->{lang}) { if (defined $self->{lang}->{$key}) { return $self->{lang}->{$key}; } } else { $self->language('/en'); if (defined $self->{lang}->{$key}) { if (@text) { for (my $i=0; $i < @text; $i++) { my $replacement = $text[$i]; $self->{lang}->{$key} =~ s/\$$i/$replacement/g; } } return $self->{lang}->{$key}; } } return undef; }
1; # End of SweetPea::Application::Locale