NAME
Apache::Language - Perl transparent language support for mod_perl scripts and Apache modules
SYNOPSIS
In YourModule.pm:
sub handler {
my $r = shift;
use Apache::Language;
my $lang = new Apache::Language ($r);
print $lang->message('Error01');
...
}
Then in YourModule.dic:
en:Error01
This is a bad thing you did!
fr:Error01
Vous venez de faire quelque chose de mauvais!
de:Error01
Fehler!
DESCRIPTION
The goal of this module is to provide a simple way for mod_perl module writers to include support for multiple language requests.
It uses the Accept-Language: field sent by the web-client, to pick the best fit language for it. It's usage is almost transparent and should prove to be quite convenient (and hopefully, efficient).
First, to use it you need to create a Apache::Language object you'll use for the lifetime of your script, whenever you need to print information that might be presented in different languages.
my $r = shift
my $lang = new Apache::Language ($r)
The first time you call that method, a specific language definitions file is parsed and language-key-content pairs are generated. That will be done only once per child httpd process.
After that it's only a matter of calling the message method like so:
print $lang->message('key');
That will produce the content for the key 'key' in the best fit language for the current request being served. That's it.
Of course you need to set-up the dictionnary file for this to work. If you are writing a module, simply name the file YourModule.dic and place it in the same place your module gets installed. If it's a Apache::Registry script, simply add a .dic to the script filename and place it in the same place. Make sure the webserver userid has read permission on the file.
The format of those Dictionnary files is pretty straight-forward
language:key
content
language:key
content
....
Just make sure those empty lines are actually empty. You should make this list balanced, all keys should be translated in the same languages. If not, sometimes Apache::Language might not be able to find a content fitting the request at all.
The message request is nothing more than a call to printf, this means you can pass it arguments and format your content anyway you want. i.e.
en:Error01
You tried to %s me and I don't think you should do %s.
then call message like so:
$lang->message("Error01", "kill", "such an evil thing");
Simple, and not as efficient as it could be, I know. :-(
Anyway, this is a first attempt at building something usefull, easy and efficient (or fast). Feedback very welcome.
TODO
Currently, the language file is only loaded once per child, then cached forever after. Last modification time should be checked so a Dictionnary file modifications would reflect instantly.
I think this could be more convenient to implement it as a tied hash, that way one module could populate a given hash with defaults values in the case that Apache::Language isn't avaliable. This with no more modification to the code than that.
SEE ALSO
perl(1), Apache(3).
SUPPORT
Please send any questions or comments to the Apache modperl mailing list <modperl@apache.org> or to me at <gozer@ectoplasm.dyndns.com>
NOTES
This code was made possible by :
Doug MacEachern <dougm@pobox.com> Creator of mod_perl. That should mean enough.
Andreas Koenig <koenig@kulturbox.de> The one I got the idea from in the first place.
The mod_perl mailing-list at <modperl@apache.org> for all your mod_perl related problems.
AUTHOR
Philippe M. Chiasson <gozer@ectoplasm.dyndns.com>
COPYRIGHT
Copyright (c) 1999 Philippe M. Chiasson. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.