translator.inc 1.92 KB
Newer Older
1
<?php
2
3
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib' . PATH_SEPARATOR . '../lang');

eric's avatar
eric committed
4
5
6
7
# this include file provides support for i18n
#

# usage:
8
#   use the __() function for returning translated strings of
eric's avatar
eric committed
9
10
11
12
13
14
15
16
17
18
19
20
21
#   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:
22
23
24
25
26
#	print __("%s has %s apples.", "Bill", "5");
#	print __("This is a %h%s%h problem!", "<b>","major","</b>");
#
# deprecated usage:
#	print __("%s has %s apples.", array("Bill", "5"));
eric's avatar
eric committed
27

28
29
include_once('config.inc');

30
31
global $_t;

32
33
34
35
36
37
38
39
40
function include_lang($trans) {
	global $LANG;

	if ($LANG != DEFAULT_LANG) {
		return include_once("$LANG/$trans");
	}
	else
		return true;
}
eric's avatar
eric committed
41

42
function __() {
eric's avatar
eric committed
43
44
45
	global $_t;
	global $LANG;

46
	# Create the translation.
47
48
49
50
51
	$args = func_get_args();

	# First argument is always string to be translated
	$tag = $args[0];

52
	if (empty($LANG) || $LANG == DEFAULT_LANG){
Loui Chang's avatar
Loui Chang committed
53
		$translated = $tag;
54
55
56
57
58
59
	}

	# If there is no translation, just print the given string.
	if (empty($_t[$tag])) {
		$translated = $tag;
	}
60
	else {
61
		$translated = $_t[$tag];
eric's avatar
eric committed
62
63
	}

Loui Chang's avatar
Loui Chang committed
64
	$translated = htmlspecialchars($translated, ENT_QUOTES);
65

66
67
68
69
70
71
72
73
74
75
76
77
78
	# This condition is to reorganise the arguments in case of
	# deprecated usage. __("string", array("string","string"))
	if (!empty($args[1]) && is_array($args[1])) {
		array_unshift($args[1], $tag);
		$args = $args[1];
	}

	$num_args = sizeof($args);

	# Subsequent arguments are strings to be formatted
	if ( $num_args > 1 ) {
		for ($i = 1; $i < $num_args; $i++) {
			$translated = preg_replace("/\%[sh]/", $args[$i], $translated, 1);
eric's avatar
eric committed
79
80
		}
	}
81

eric's avatar
eric committed
82
83
84
	return $translated;
}