# NAME I18N::Handle - A common i18n handler for web frameworks and applications. # ***THIS MODULE IS STILL IN DEVELOPMENT*** # DESCRIPTION [I18N::Handle](http://search.cpan.org/perldoc?I18N::Handle) is a common handler for web frameworks and applications. You can use [App::I18N](http://search.cpan.org/perldoc?App::I18N) to generate po/mo files, then use this module to handle these languages. # SYNOPSIS Ideas are welcome. just drop me a line. option `import` takes the same arguments as [Locale::Maketext::Lexicon](http://search.cpan.org/perldoc?Locale::Maketext::Lexicon) takes. it's _language_ => [ _format_ => _source_ ]. use I18N::Handle; my $hl = I18N::Handle->new( import => { en => [ Gettext => 'po/en.po' ], fr => [ Gettext => 'po/fr.po' ], jp => [ Gettext => 'po/jp.po' ], })->accept( qw(en fr) )->speak( 'en' ); Or a simple way to import gettext po files: This will transform the args to the args that `import` option takes: use I18N::Handle; my $hl = I18N::Handle->new( Gettext => { en => 'po/en.po', fr => 'po/fr.po', jp => [ 'po/jp.po' , 'po2/jp.po' ], })->accept( qw(en fr) )->speak( 'en' ); print _('Hello world'); $hl->speak( 'fr' ); $hl->speak( 'jp' ); $hl->speaking; # return 'jp' my @langs = $hl->can_speak(); # return 'en', 'fr', 'jp' # USE CASES ## Handling po files $hl = I18N::Handle->new( po => 'path/to/po', style => 'gettext' # use gettext style format (default) )->speak( 'en' ); print _('Hello world'); ## Handling locale If you need to bind the locale directory structure like this: po/en/LC_MESSAGES/app.po po/en/LC_MESSAGES/app.mo po/zh_tw/LC_MESSAGES/app.po po/zh_tw/LC_MESSAGES/app.mo You can just pass the `locale` option: $hl = I18N::Handle->new( locale => 'path/to/locale' )->speak( 'en_US' ); ## Singleton If you need a singleton [I18N::Handle](http://search.cpan.org/perldoc?I18N::Handle), this is a helper function to return the singleton object: $hl = I18N::Handle->singleton( locale => 'path/to/locale' ); In your applications, might be like this: sub get_i18n { my $class = shift; return I18N::Handle->singleton( ... options ... ) } ## Connect to a remote i18n server __not implemented yet__ Connect to a translation server: $handle = I18N::Handle->new( server => 'translate.me' )->speak( 'en_US' ); ## Binding with database __not implemented yet__ Connect to a database: $handle = I18N::Handle->new( dsn => 'DBI:mysql:database=$database;host=$hostname;port=$port;' ); ## Binding with Google translation service __not implemented yet__ Connect to google translation: $handle = I18N::Handle->new( google => "" ); # OPTIONS - _format_ => { _language_ => _source_ , ... } Format could be _Gettext | Msgcat | Slurp | Tie_. use I18N::Handle; my $hl = I18N::Handle->new( Gettext => { en => 'po/en.po', fr => 'po/fr.po', jp => [ 'po/jp.po' , 'po2/jp.po' ], }); $hl->speak( 'en' ); - `po` => '_path_' | [ _path1_ , _path2_ ] Suppose you have these files: po/en.po po/zh_TW.po When using: I18N::Handle->new( po => 'po' ); will be found. can you can get these langauges: [ en , zh-tw ] - `locale` => 'path' | [ path1 , path2 ] - `import` => Arguments to [Locale::Maketext::Lexicon](http://search.cpan.org/perldoc?Locale::Maketext::Lexicon) # OPTIONAL OPTIONS - `style` => _style_ ... (Optional) The style could be `gettext`. - `loc` => _global loc function name_ (Optional) The default loc function name is `_`. # PUBLIC METHODS ## new ## singleton( I ) If you need a singleton [I18N::Handle](http://search.cpan.org/perldoc?I18N::Handle), this is a helper function to return the singleton object. ## speak( I ) setup current language. _language_, can be `en`, `fr` and so on.. ## speaking() get current speaking language name. ## can_speak() return a list that currently supported. ## accept( I ) setup accept languages. $hl->accpet( qw(en fr) ); ## fallback( I ) setup fallback language. when speak() fails , fallback to this language. $hl->fallback( 'en' ); # PRIVATE METHODS ## _unify_langtag ## _scan_po_files ## _scan_locale_files # AUTHOR Yoan Lin # SEE ALSO [App::I18N](http://search.cpan.org/perldoc?App::I18N) # LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.