File tree 4 files changed +32
-4
lines changed
4 files changed +32
-4
lines changed Original file line number Diff line number Diff line change 21
21
)
22
22
);
23
23
24
+ // Get current date
25
+ $ currentDate = new DateTime ();
26
+ $ year = $ currentDate ->format ('Y ' ); // YYYY
27
+ $ month = $ currentDate ->format ('m ' ); // MM
28
+ $ day = $ currentDate ->format ('d ' ); // dd
29
+
30
+ // Example usage
31
+ $ params = [$ year , $ month , $ day ];
32
+
24
33
try {
25
34
$ i18n = new I18n ($ config );
26
35
$ i18n ->setLangAlias (array ('en_US ' => 'English ' , 'zh_TW ' => '繁體中文 ' ));
27
36
echo '<h2>Fetch \'hello \' from \'greeting.json \'</h2> ' ;
28
37
echo $ i18n ->fetch ('greeting.hello ' );
29
- echo $ i18n ->fetch ('greeting.world ' );
38
+ echo $ i18n ->fetch ('greeting.world ' ).'<br> ' ;
39
+ echo $ i18n ->fetch ('general.today ' , $ params );
30
40
echo '<h2>Fetch Current Language</h2> ' ;
31
41
echo $ i18n ->fetchCurrentLang ();
32
42
echo '<h2>Fetch List</h2> ' ;
Original file line number Diff line number Diff line change 1
1
{
2
- "welcome_message" : " Welcome to I18n!"
2
+ "welcome_message" : " Welcome to I18n!" ,
3
+ "today" : " Today is {0}/{1}/{2}"
3
4
}
Original file line number Diff line number Diff line change 1
1
{
2
- "welcome_message" : " 歡迎使用I18n!"
2
+ "welcome_message" : " 歡迎使用I18n!" ,
3
+ "today" : " 今天是{0}/{1}/{2}"
3
4
}
Original file line number Diff line number Diff line change @@ -74,7 +74,7 @@ public function getLangAlias(): array
74
74
return $ this ->fetchLangList ();
75
75
}
76
76
77
- public function fetch (string $ key ): ?string
77
+ public function fetch (string $ key, ? array $ param = null ): ?string
78
78
{
79
79
if (!$ this ->initialized ) {
80
80
throw new InitializationException ('I18n class must be initialized before using fetch(). ' );
@@ -86,6 +86,10 @@ public function fetch(string $key): ?string
86
86
$ value = $ this ->languageLoader ->getValue (self ::$ option ['defaultLang ' ], $ key );
87
87
}
88
88
89
+ if ($ param !== null ) {
90
+ $ value = self ::langParam ($ value , $ param );
91
+ }
92
+
89
93
return $ value ;
90
94
}
91
95
@@ -116,6 +120,18 @@ public function fetchCurrentLang(): string
116
120
return $ this ->currentLang ;
117
121
}
118
122
123
+ public static function langParam (string $ value , array $ param ): string
124
+ {
125
+ $ replacements = [];
126
+
127
+ // Construct replacement array
128
+ foreach ($ param as $ index => $ p ) {
129
+ $ replacements ['{ ' . $ index . '} ' ] = $ p ; // Placeholder as key
130
+ }
131
+
132
+ return strtr ($ value , $ replacements );
133
+ }
134
+
119
135
private function validateLanguageFolder (string $ folder ): string
120
136
{
121
137
$ folderPath = self ::$ option ['langFilePath ' ].Utils::DIR_SEP .$ folder ;
You can’t perform that action at this time.
0 commit comments