summaryrefslogtreecommitdiffstats
path: root/web/lib/translator.inc
blob: 79eb39c7857274ba9addeb3dbc5afea882817227 (plain)
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
<?
# this include file provides support for i18n
#

# usage:
#   use the _() function for returning translated strings of
#   text.  The string can contain escape codes %h for HTML
#   and %s for regular text.
#
# supporting scripts:
#   there is a supporting script, web/utils/genpopo, that will
#   parse the PHP files and create PHP include files that contain
#   a mapping for each translated language.  The include files
#   have the form,
#
#     $_t["en"]["My cat is large."] = "My cat is large.";
#     $_t["es"]["My cat is large."] = "Mi gato esta grande.";
#
# examples:
#   print _("%s has %s apples.", array("Bill", "5"));
#   print _("This is a %h%s%h problem!", array("<b>","major","</b>"));

include_once("common_po.inc");

function _($tag, $args=array()) {
	global $_t;
	global $_REQUEST;
	global $LANG;

	$supported_langs = array(
		"en" => 1, # English
		"es" => 1, # Español
		"de" => 1, # Deutsch
		"fr" => 1, # Français
	);

	# default to English if the lang hasn't been provided or isn't supported
	#
	$LANG = $_REQUEST['LANG'];
	if (!$LANG || !array_key_exists($LANG, $supported_langs)) {
		$LANG = "en";
	}

	# create the translation, if it doesn't exist, highlight it
	#
	$translated = $_t[$LANG][$tag];
	if (!$translated) {
		# if it's a supported language, but there isn't a translation,
		# alert the visitor to the missing translation.
		#
		$translated = "<blink><b>_" . $tag . "_</b></blink>";
	}

	# replace escape substitutions
	#
	if (!empty($args)) {
		while (list($k, $v) = each($args)) {
			$translated = preg_replace("/\%[sh]/", $v, $translated, 1);
		}
	}
	return $translated;
}

# vim: ts=2 sw=2 noet ft=php
?>