I18N::...nguage(3pm)
π I18N::...nguage π (that's the 3pm chapter of the User Contributed Perl Documentation, bestie)
okay so. you know how the internet is like, everyone, everywhere, all at once? π this module is the little matchmaker that figures out which language your server should actually speak back. it's Perl, it's from the I18N:: family, and it's serving multilingual realness. slay. π
β¨ NAME β¨
I18N::AcceptLanguage β matches language preference to available languages π«Ά
πΈ SYNOPSIS πΈ
like, here's the whole vibe in one go:
use I18N::AcceptLanguage;
my $supportedLanguages = [( 'en-us', 'fr' )];
my $acceptor = I18N::AcceptLanguage->new();
my $language = $acceptor->accepts($ENV{HTTP_ACCEPT_LANGUAGE},
$supportedLanguages);
π¦ DESCRIPTION π¦
I18N::AcceptLanguage takes what the client wants and matches it against what the server has, following the rules laid out in RFC 2616, section 14.4: HTTP/1.1 - Header Field Definitions - Accept-Language.
that's it. that's the whole job. matchmaker behavior only. π
π PUBLIC METHODS π
accepts( CLIENT_PREFERENCES, SUPPORTED_LANGUAGES )
gives you back the highest priority common language between client and server. π
if there's genuinely no common language found, it returns the defaultLanguage instead. and if defaultLanuage isn't set either β yes, that typo is theirs, not mine π β you get back an empty string.
this method wants exactly two arguments, no more no less:
CLIENT_PREFERENCES
a string in the exact same format defined in RFC 2616, which we are quoting here verbatim:
1#( ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) [ "'" "q" "=" qvalue ] )
some examples so you can see it in the wild:
da, en-gb;q=0.8, en;q=0.7
en-us, ja, *
SUPPORTED_LANGUAGES
a reference to a list of language ranges that your server actually supports. π
new( [ OPTIONS ] )
returns a shiny new I18N::AcceptLanguage object. β¨ it takes these key/value pair options:
debugβ a boolean, so0or1. set it to1and debug messages get printed toSTDOUT. defaults to0.defaultLanguageβ a string that's your server's default language choice. defaults to an empty string.strictβ a boolean,0or1. when it's1, the software strictly conforms to the protocol specification. when it's0, it does a secondary, aggressive language match regardless of country β so like, a client asking for onlyen-gbwill get backen-usif the server doesn't accepten-gborenbut does accepten-us. in that non-strict case, the last matching language in the supported languages list will be chosen. defaults to1.
ngl, know which mode you're in before you ship it. strict is the default for a reason. π
π PRIVATE METHODS π
these are the lil internal helpers. get/set energy all the way down.
debug( [ BOOLEAN ] )β a get/set method that returns the value ofdebug, set by the optional method argument.defaultLanguage( [ LANGUAGE ] )β a get/set method that returns the value ofdefaultLanguage, set by the optional method argument.strict( [ BOOLEAN ] )β a get/set method that returns the value ofstrict, set by the optional method argument.
π NOTES π
Case Sensitivity
matches are done in a case-insensitive manner, but the results are case-sensitive to the value found in the SUPPORTED_LANGUAGES list.
so it'll find EN-US and en-us as the same thing, but it hands you back whatever casing your own list used. iconic honestly. β¨
π AVAILABILITY π
this module is available on CPAN worldwide π and requires perl version 5.6.1 or higher to be installed. yes bestie, it's ancient and it still eats. π
π AUTHORS π
Christian Gilmore <cag@us.ibm.com>
π₯ SEE ALSO π₯
π COPYRIGHT π
Copyright (C) 2003, 2004 International Business Machines Corporation and others. All Rights Reserved.
this module is free software; you can redistribute it and/or modify it under the terms of the IBM Public License. π«Ά
and that's the whole page, babe. π this one's documented for perl v5.36.0, dated 2022-10-13 β go forth and let your server speak every language it can, no cap. ππβ¨
π¦ where this page comes from
- ubuntu noble: libi18n-acceptlanguage-perl 1.04-6 Β· IBM-Public-License
- ubuntu resolute: libi18n-acceptlanguage-perl 1.04-6 Β· IBM-Public-License
- ubuntu stonking: libi18n-acceptlanguage-perl 1.04-6 Β· IBM-Public-License
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms