File: Language.pm

package info (click to toggle)
pnopaste 1.4-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 340 kB
  • ctags: 66
  • sloc: perl: 1,043; sh: 42; sql: 37; makefile: 5
file content (109 lines) | stat: -rw-r--r-- 2,001 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Language;

# This file is part of the pnopaste program
# Copyright (C) 2008-2010 Patrick Matthäi <pmatthaei@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

use warnings;
use strict;
use Geo::IP;
use CGI qw/:standard/;


# Default language.
my $Language = 'EN';
my $Lang_Dir = 'lang/';

# Start the language detection.
Initialiaze();


### Initialize the remote client.
sub Initialiaze {
	# Our CGI object.
	my $CGI = new CGI;
	my $Remote_Addr = $CGI->remote_host();

	if(!defined $Remote_Addr or $Remote_Addr eq 'localhost'){
		# Could not get the IP address, we set the default and return.
		Set_Language($Language);
		return;
	}

	# Our GeoIP object with the standard country database.
	my $GeoIP = Geo::IP->new(GEOIP_STANDARD);

	# Get country code of the remote client.
	my $Country = $GeoIP->country_code_by_addr($Remote_Addr);

	Set_Language($Country);
}


### Sets the language based on the result of geoip country code.
sub Set_Language {
	my($Lang) = @_;

	$Lang = Mapping($Lang);

	my $Lang_File = $Lang_Dir . $Lang . '.pm';

	if(-e $Lang_File){
		# We have got a translation for this client.
		$Language = $Lang;
	}

}


### Do some static language country code mappings.
sub Mapping {
	my($Lang) = @_;

	if(!defined $Lang){
		$Lang = 'EN';
	}
	elsif($Lang eq 'AT'){
		$Lang = 'DE';
	}

	return $Lang;
}


### Get all available translations.
sub Get_Languages {
	my @Languages = ();

	opendir(R, $Lang_Dir) or return undef;

	while(my $File = readdir(R)){
		if($File =~ /^.*\.pm$/){
			$File =~ s/\.pm//;

			push(@Languages, $File);
		}
	}

	closedir(R);

	return @Languages;
}


### Get the translation for the translateable ID.
sub Get {
	my($ID) = @_;

	# Load language module.
	require $Lang_Dir . $Language . '.pm';

	return $Translation::Strings{$ID};
}


1;