The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Locale::Maketext::TieHash::L10N - Tying language handle to a hash

SYNOPSIS

 use strict;
 use Locale::Maketext::TieHash::L10N;
 my %mt;
 { use MyProgram::L10N;
   my $lh = MyProgram::L10N->get_handle() || die "What language?";
   # configure language handle and option numf_comma
   tie %mt, 'Locale::Maketext::TieHash::L10N', L10N => $lh, numf_comma => 1;
 }
 ...
 print qq~$mt{Example}:\n$mt{["Can't open file [_1]: [_2].", $f, $!]}\n~;

The way without this module - You better see the difference.

 use strict;
 use MyProgram::L10N;
 my $lh = MyProgram::L10N->get_handle() || die "What language?";
 $lh{numf_comma} = 1;
 ...
 print $lh->maketext('Example') . ":\n" . $lh->maketext("Can't open file [_1]: [_2].", $f, $!) . "\n";

Example for writing HTML

 use strict;
 use Locale::Maketext::TieHash::L10N;
 my %mt;
 { use MyProgram::L10N;
   my $lh = MyProgram::L10N->get_handle() || die "What language?";
   # tie and configure
   tie %mt, 'Locale::Maketext::TieHash::L10N',
     L10N       => $lh,   # save language handle
     numf_comma => 1,     # set option numf_comma
     nbsp_flag  => '~',   # set nbsp_flag to '~'
     # If you want to test your Script,
     # you set "nbsp" on a string which you see in the Browser.
     nbsp       => '<span style="color:red">§</span>',
   ;
 }
 ...
 # The browser shows value and unity always on a line.
 print qq#$mt{["Put [*,_1,~component,~components,no component] together, then have [*,_2,~piece,~pieces,no piece] of equipment.", $component, $piece]}\n#;

read Configuration

 my %config = tied(%mt)->Config();

write Configuration

 my %config = tied(%mt)->Config(numf_comma => 0, nbsp_flag => '');

DESCRIPTION

Object methods like "maketext" don't have interpreted into strings. The module ties the language handle to a hash. The object method "maketext" is executed at fetch hash. At long last this is the same, only the notation is shorter.

Sometimes the object method "maketext" expects more than 1 parameter. Then submit a reference on an array as hash key.

If you write HTML text with "Locale::Maketext", it then can happen that value and unity stand on separate lines. The "nbsp_flag" prevents the line break. The "nbsp_flag" per default is undef and this functionality is switched off. Set your choice this value on a character string. For switching the functionality off, set the value to undef or a character string of the length 0. "nbsp" per default is "&nbsp;".

METHODS

TIEHASH

 use Locale::Maketext::TieHash::L10N;
 tie my %mt, 'Locale::Maketext::TieHash::L10N', %config;

"TIEHASH" ties your hash and set options defaults.

Config

"Config<"> configures the language handle and/or options.

 # configure the language handle
 tied(%mt)->Config(L10N => $lh);

 # configure option of language handle
 tied(%mt)->Config(numf_comma => 1);
 # the same is:
 $lh->{numf_comma} = 1;

 # only for debugging your HTML response
 tied(%mt)->Config(nbsp => 'see_position_of_nbsp_in_HTML_response');   # default is '&nbsp;'

 # Set a flag to say:
 #  Substitute the whitespace before this flag and this flag to '&nbsp;' or your debugging string.
 # The "nbsp_flag" is a string (1 or more characters).
 tied(%mt)->Config(nbsp_flag => '~');

The method calls croak, if the key of your hash is undef or your key isn't correct and if the value, you set to option "nbsp", is undef.

"Config" accepts all parameters as Hash and gives a Hash back with all attitudes.

FETCH

"FETCH" translate the given key of your hash and give back the translated string as value.

 # translation
 print $mt{'you write this language'};
 # the same is:
 print $lh->maketext('you write this language');
 ...
 print $mt{['Put [*,_1,component,components,no component] together.', $number]};
 # the same is:
 print $lh->maketext('Put [*,_1,component,components,no component] together.', $number);
 ...
 # Use "nbsp" and the "nbsp_flag" is true.
 print $mt{['Put [*,_1,~component,~components,no component] together.', $number]};
 # the same is:
 my $translation = $lh->maketext('Put [*,_1,~component,~components,no component] together.', $number);
 $tanslation =~ s/ ~/&nbsp;/g;   # But not a global debugging function is available.

The method calls croak, if the method "maketext" of your stored language handle dies.

STORE (deprecated, see Config)

"STORE" stores the language handle or options.

 # store the language handle
 $mt{L10N} = $lh;

 # store option of language handle
 $mt{numf_comma} = 1;
 # the same is:
 $lh->{numf_comma} = 1;

 # only for debugging your HTML response
 $mt{nbsp} = 'see_position_of_nbsp_in_HTML_response';   # default is '&nbsp;'

 # Set a flag to say:
 #  Substitute the whitespace before this flag and this flag to '&nbsp;' or your debugging string.
 # The "nbsp_flag" is a string (1 or more characters).
 $mt{nbsp_flag} = '~';

The method calls croak, if the key of your hash is undef or your key isn't correct and if the value, you set to option "nbsp", is undef.

Keys (depreacted, see Config)

Get all keys back.

Values (deprecated, see Config)

Get all values back.

Get (deprecated, see Config)

Submit 1 key or more. The method "Get" give you the values back.

The method calls croak if a key is undef or unknown.

SEE ALSO

Tie::Hash

Locale::Maketext

AUTHOR

Steffen Winkler, <cpan@steffen-winkler.de>

COPYRIGHT AND LICENSE

Copyright (C) 2004, 2005 by Steffen Winkler

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.6.1 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 134:

Non-ASCII character seen before =encoding in 'style="color:red">§</span>','. Assuming CP1252