4
4
* i18n dummy we'll be happy to have later
5
5
*/
6
6
function __ ($ msg , $ args = [])
7
- {
8
- return strtr ($ msg , $ args );
7
+ {
8
+ return strtr (i18n:: translate ( $ msg) , $ args );
9
9
}
10
10
11
11
/**
@@ -15,9 +15,43 @@ function __($msg, $args = [])
15
15
*/
16
16
class i18n
17
17
{
18
- public static function register () /*needed to trigger class include, presumably setup would happen here*/
18
+ protected static
19
+ $ language = null ,
20
+ $ translations = [],
21
+ $ country = null ;
22
+
23
+ public static function register ($ culture = null ) /*needed to trigger class include, presumably setup would happen here*/
24
+ {
25
+ if ($ culture === null )
26
+ {
27
+ $ urlTokens = $ _SERVER ['HTTP_HOST ' ] ? explode ('. ' , $ _SERVER ['HTTP_HOST ' ]) : [];
28
+ $ code = $ urlTokens ? reset ($ urlTokens ) : 'en ' ;
29
+ switch ($ code )
30
+ {
31
+ case 'pt ' :
32
+ $ culture = 'pt_PT ' ; break ;
33
+ case 'en ' :
34
+ case 'www ' :
35
+ default :
36
+ $ culture = 'en_US ' ;
37
+ }
38
+ }
39
+
40
+ list ($ language , $ country ) = explode ('_ ' , $ culture );
41
+ static ::$ language = $ language ;
42
+ static ::$ country = $ country ;
43
+
44
+ setlocale (LC_MONETARY , $ culture . '.UTF-8 ' );
45
+ }
46
+
47
+ public static function getLanguage ()
19
48
{
20
- setlocale (LC_MONETARY , 'en_US.UTF-8 ' );
49
+ return static ::$ language ;
50
+ }
51
+
52
+ public static function getCountry ()
53
+ {
54
+ return static ::$ country ;
21
55
}
22
56
23
57
public static function formatCurrency ($ amount , $ currency = 'USD ' )
@@ -29,4 +63,31 @@ public static function formatCredits($amount)
29
63
{
30
64
return '<span class="formatted-credits"> ' . number_format ($ amount , 1 ) . ' LBC</span> ' ;
31
65
}
66
+
67
+ public static function translate ($ token , $ language = null )
68
+ {
69
+ $ language = $ language === null ? static ::$ language : $ language ;
70
+ if (!isset (static ::$ translations [$ language ]))
71
+ {
72
+ $ path = ROOT_DIR . '/data/i18n/ ' . $ language . '.yaml ' ;
73
+ static ::$ translations [$ language ] = file_exists ($ path ) ? Spyc::YAMLLoadString (file_get_contents ($ path )) : [];
74
+ }
75
+ $ scope = static ::$ translations [$ language ];
76
+ foreach (explode ('. ' , $ token ) as $ level )
77
+ {
78
+ if (isset ($ scope [$ level ]))
79
+ {
80
+ $ scope = $ scope [$ level ];
81
+ }
82
+ else
83
+ {
84
+ $ scope = [];
85
+ }
86
+ }
87
+ if (!$ scope && $ language != 'en ' )
88
+ {
89
+ return static ::translate ($ token , 'en ' );
90
+ }
91
+ return $ scope ?: $ token ;
92
+ }
32
93
}
0 commit comments