-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage.php
More file actions
115 lines (96 loc) · 2.31 KB
/
language.php
File metadata and controls
115 lines (96 loc) · 2.31 KB
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
110
111
112
113
114
<?php
namespace BestProject\Wordpress {
defined('ABSPATH') or die;
use BestProject\Wordpress\Theme;
/**
* Localization class. Takes care of translating strings constants into a proper language.
*/
class Language
{
/**
* Holds all translations and its keys.
*
* @var Array
*/
protected static $strings;
/**
* Holds current language code (eg. eb_GB)
*
* @var String
*/
protected static $code;
/**
* Name of current theme.
*
* @var String
*/
protected static $themeName;
/**
* Load all translations into memmory.
*/
protected static function load()
{
if (is_null(self::$code)) {
self::$code = get_locale();
}
/* @var $theme Theme */
$path = get_stylesheet_directory();
$filename = $path.'/languages/'.self::$code.'.ini';
if (!file_exists($filename)) {
$filename = $path.'/languages/en_GB.ini';
}
if (file_exists($filename)) {
self::$strings = parse_ini_file($filename);
}
}
/**
* Returns a translated version of provided string. If more then 1 attribute is provided `sprintf` will be used.
*
* @param String $string Key that represents the translation.
* @return String
*/
public static function _($string)
{
if (is_null(self::$strings)) {
self::load();
}
if (is_null(self::$themeName)) {
self::$themeName = Theme::getName();
}
if (func_num_args() === 1) {
if (isset(self::$strings[$string])) {
return self::$strings[$string];
} else {
return __($string, self::$themeName);
}
} else {
if (isset(self::$strings[$string])) {
$args = func_get_args();
array_shift($args);
array_unshift($args, self::$strings[$string]);
return call_user_func_array('sprintf', $args);
} else {
$args = func_get_args();
$args = array_push($args, self::$themeName);
return call_user_func_array('__', $args);
}
}
}
}
}
namespace {
/**
* Translating text through \BestProject\Wordpress\Language with fallback to original
* "_" function. Just something to be used as a shortcut in templates.
* If more then 1 attribute is provided `sprintf` will be used.
*
* @return String
*/
function t($string)
{
if (!is_null($string)) {
return call_user_func_array('BestProject\Wordpress\Language::_',
func_get_args());
}
}
}