NAME
Locale::Babelfish - wrapper between Locale::Maketext::Lexicon and https://github.com/nodeca/babelfish format
VERSION
This document describes version 0.01 of Locale::Babelfish
DESCRIPTION
Internationalisation with easy syntax. Simple wrapper between Locale::Maketext and https://github.com/nodeca/babelfish format. Created for using same dictionaries on backend and frontend.
SYNOPSIS
package Foo;
use Locale::Babelfish;
my $bf = Locale::Babelfish->new( { dirs => [ '/path/to/dictionaries'] } );
warn $bf->t('dictionary.firstkey.nextkey', { foo => 'bar'} );
More sophisticated example:
package Foo::Bar;
use Locale::Babelfish;
...
my $bf = Locale::Babelfish->new( {
dirs => [ '/path/to/dictionaries'],
default_lang => ['ru_RU'], # By default en_US
langs => [{ 'uk_UA' => 'Foo::Bar::Lang::uk_UA' } , 'de_DE' ] # for custom languages specify they are plural forms
},
$logger # Logger for example Log::Log4Perl, not required parameter
);
warn $bf->t('dictionary.firstkey.nextkey', { foo => 'bar'} );
$bf->set_context_lang('en_US');
warn $bf->t('dictionary.firstkey.nextkey', { foo => 'bar'} );
Phrases Syntax
#{varname} Echoes value of variable ((Singular|Plural1|Plural2)):count Plural form
Example:
I have #{count} ((nail|nails)):count
or short form
I have #{count} ((nail|nails))
dictionary file example
Module support only yaml format. create dictionary file like: dictionary.en_US.yaml where dictionary - is name of dictionary and en_US - his locale
profile: Profiel apps: forums: new_topic: New topic last_post: title : Last message demo: apples: I have #{count} ((apple|apples))
Custom plural forms
By default locale will be inherited from en_US. If you would like specify own, create module like this: and implement quant_word function.
..... package Locale::Babelfish::Lang::uk_UA;
use parent 'Locale::Babelfish::Maketext'; use strict;
sub quant_word { my ($self, $num, $single, $plural1, $plural2) = @_;
my $num_s = $num % 10;
my $num_dec = $num % 100;
my $ret;
if ($num_dec >= 10 and $num_dec <= 20) { $ret = $plural2 || $plural1 || $single }
elsif ($num_s == 1) { $ret = $single }
elsif ($num_s >= 2 and $num_s <= 4) { $ret = $plural1 || $single }
else { $ret = $plural2 || $plural1 || $single }
return $ret;
}
1; ......
Dictionary encoding
Use any convinient encoding.
Methods
set_context_lang
$self->set_context_lang( 'ru_RU' );
Setting current context.
check_dictionaries
$self->check_dictionaries();
check what changed at dictionaries
t
Get internationalized value for key from dictionary.
$self->t( 'main.key.subkey' , { paaram1 => 1 , param2 => { next_level => 'test' } } );
Where main - is dictionary, key.subkey - key at dictionary
has_any_value
$self->has_any_value( 'main.key.subkey' );
Check exist or not key in dictionary.
Where main - is dictionary, key.subkey - key at dictionary
maketext
$self->maketext( 'dict', 'key' , $param1, ... $paramN );
SEE ALSO
Locale::Maketext::Lexicon, https://github.com/nodeca/babelfish
AUTHORS
Mironov Igor <grif@cpan.org<gt>,
Crazy Panda LLC,
REG.RU LLC
COPYRIGHT
This software is released under the MIT license cited below. Additionally, when this software is distributed with Perl Kit, Version 5, you may also redistribute it and/or modify it under the same terms as Perl itself.
The "MIT" License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.