Skip to content

Instantly share code, notes, and snippets.

@khardix
Created April 7, 2018 18:19
Show Gist options
  • Save khardix/2a7279ebb13d6011dd74222766812cb5 to your computer and use it in GitHub Desktop.
Save khardix/2a7279ebb13d6011dd74222766812cb5 to your computer and use it in GitHub Desktop.
nginx perl module for processing Accept-Language HTTP header
package http_lang;
use nginx;
sub quality {
my $lang = shift;
if ($lang =~ /;q=(\d\.\d+)$/) {
return $1;
}
else {
return 1.0;
}
}
sub lang_only {
my $lang = shift;
if ($lang =~ /^([a-z]{2})[-;]/) {
return $1;
}
else {
return $lang;
}
}
sub choose {
my @requested = split(/,\s*/, shift);
my @supported = split(/,\s*/, shift);
my @order = sort { quality($b) <=> quality($a) } @requested;
foreach my $lang (@order) {
$lang = lang_only($lang);
return $lang if ($lang ~~ @supported);
}
return $supported[0];
}
sub handler {
my $r = shift;
my $supported = $r->variable('SUPPORTED_LANG') or "en";
my $requested = $r->header_in('Accept-Language') or "en-US";
my $lang = choose($requested, $supported);
$r->internal_redirect('/' . $lang . $r->uri);
return OK;
}
sub debug {
my $r = shift;
my $supported = $r->variable('SUPPORTED_LANG') or "en";
my $requested = $r->header_in('Accept-Language') or "en-US";
my $lang = choose($requested, $supported);
$r->send_http_header("text/plain");
return OK if $r->header_only;
$r->print('$supported = ' . $supported . "\n");
$r->print('$requested = ' . $requested . "\n");
$r->print('Redirect to: ' . '/' . $lang . $r->uri . "\n");
return OK;
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment