<commit_before>#!/usr/bin/perl -w
#
# mySociety/Locale.pm:
# I18n with Perl
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/
#
# $Id: Locale.pm,v 1.3 2007-05-01 15:32:56 matthew Exp $

package mySociety::Locale;

use strict;
use HTTP::Negotiate;
use Locale::gettext;
use POSIX qw(setlocale LC_ALL);

my $gettext;

# Note, that this function is forced into the main namespace
# as Perl treats the _ function as magic
sub _ {
    return $gettext->get($_[0]);
}

our %langmap = ();
our %langs = ();
our $lang;

# negotiate_language CONFIG OVERRIDE
# Sets $lang to negotiated language.
# CONFIG is string from config file containing list of available languages. 
#        e.g. 'en-gb,English,en_GB|pt-br,Portugu&ecirc;s (Brasil),pt_BR'
# OVERRIDE is override language, such as from cookie or domain name.  Set to
# null to force negotiation of language from browser, using HTTP headers. */
sub negotiate_language($;$) {
    my ($available_language_config, $override_language) = @_;
    my @opt_langs = split /\|/, $available_language_config;
    my $variants = [];
    foreach my $opt_lang (@opt_langs) {
        my ($code, $verbose, $locale) = split /,/, $opt_lang;
        $langs{$code} = $verbose;
        $langmap{$code} = $locale;
	push @$variants, [$code, undef, undef, undef, undef, $code, undef];
    }
    if ($override_language && $langs{$override_language}) {
        $lang = $override_language;
    } else {
        $lang = HTTP::Negotiate::choose($variants);
        if (!$lang || !$langmap{$lang}) {
            $lang = 'en-gb'; # Default override
        }
    }
    return $lang;
}

# change LANG
# Change human language to display text, dates, numbers etc. in. LANG is the
# keys from the available language string previously passed to
# negotiate_language. Leave unset to use the default negotiated language.
my $current = '';
sub change(;$) {
    my $l = shift || '';
    $l = $lang if $l eq "";
    return if $l eq $current;
    my $os_locale = $langmap{$l}.'.UTF-8';
    delete $ENV{LANGUAGE}; # clear this if set
    $ENV{LANG} = $os_locale;
    my $ret = setlocale(LC_ALL, $os_locale);
    die "setlocale failed for $os_locale" if $ret ne $os_locale;<commit_msg>Show category and whether map was used; tabs.<commit_after>#!/usr/bin/perl -w
#
# mySociety/Locale.pm:
# I18n with Perl
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: <EMAIL>; WWW: http://www.mysociety.org/
#
# $Id: Locale.pm,v 1.4 2007-05-16 10:55:46 matthew Exp $

package mySociety::Locale;

use strict;
use HTTP::Negotiate;
use Locale::gettext;
use POSIX qw(setlocale LC_ALL);

my $gettext;

# Note, that this function is forced into the main namespace
# as Perl treats the _ function as magic
sub _ {
    return $gettext->get($_[0]);
}

our %langmap = ();
our %langs = ();
our $lang;

# negotiate_language CONFIG OVERRIDE
# Sets $lang to negotiated language.
# CONFIG is string from config file containing list of available languages. 
#        e.g. 'en-gb,English,en_GB|pt-br,Portugu&ecirc;s (Brasil),pt_BR'
# OVERRIDE is override language, such as from cookie or domain name.  Set to
# null to force negotiation of language from browser, using HTTP headers. */
sub negotiate_language($;$) {
    my ($available_language_config, $override_language) = @_;
    my @opt_langs = split /\|/, $available_language_config;
    my $variants = [];
    foreach my $opt_lang (@opt_langs) {
        my ($code, $verbose, $locale) = split /,/, $opt_lang;
        $langs{$code} = $verbose;
        $langmap{$code} = $locale;
        push @$variants, [$code, undef, undef, undef, undef, $code, undef];
    }
    if ($override_language && $langs{$override_language}) {
        $lang = $override_language;
    } else {
        $lang = HTTP::Negotiate::choose($variants);
        if (!$lang || !$langmap{$lang}) {
            $lang = 'en-gb'; # Default override
        }
    }
    return $lang;
}

# change LANG
# Change human language to display text, dates, numbers etc. in. LANG is the
# keys from the available language string previously passed to
# negotiate_language. Leave unset to use the default negotiated language.
my $current = '';
sub change(;$) {
    my $l = shift || '';
    $l = $lang if $l eq "";
    return if $l eq $current;
    my $os_locale = $langmap{$l}.'.UTF-8';
    delete $ENV{LANGUAGE}; # clear this if set
    $ENV{LANG} = $os_locale;
    my $ret = setlocale(LC_ALL, $os_locale);
    die "setlocale failed for $os_locale" if $ret ne $os_locale;
{'fraction_non_alphanumeric': 0.13893163332600572, 'fraction_numerical': 0.010551769619696637, 'mean_word_length': 3.4607843137254903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3699301', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1493, 'n_words': 482}