forked from RichardMN/pico_multilanguage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiLanguage.php
394 lines (346 loc) · 13.9 KB
/
MultiLanguage.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Pico MultiLanguage plugin - multi-language support for Pico CMS (https://picocms.org/)
*
* @author Til Boerner <[email protected]>, Richard Martin-Nielsen <[email protected]>, Iridescent <[email protected]>
* @link https://github.com/tilboerner/pico_languages
* @link https://github.com/RichardMN/pico_multilanguage
* @link https://github.com/iridescent-dev/pico-multilanguage-plugin
* @license http://opensource.org/licenses/MIT The MIT License
* @copyright 2014 Til Boerner
* @version 1.0
*/
class MultiLanguage extends AbstractPicoPlugin
{
/**
* This plugin is enabled by default
*
* @see AbstractPicoPlugin::$enabled
* @var boolean
*/
protected $enabled = true;
// The default language
private $default_language = 'en';
// The list of available languages and their display names
private $languages = array('en' => 'English');
// The list of available language codes
private $available_languages = array();
// The path to your language directory
private $language_dir = 'language/';
// The list of translated titles of your website
private $site_titles = array();
// The list of date formats by languages
private $date_formats = array();
// The list of date locales by languages
private $date_locales = array();
// Array of page data grouped by language
private $pages_by_language = array();
// Array of page data grouped by id
private $pages_by_id = array();
// The current language
private $current_language;
/**
* Triggered after Pico has read its configuration
*
* @see Pico::getConfig()
* @see Pico::getBaseUrl()
* @see Pico::isUrlRewritingEnabled()
*
* @param array &$config array of config variables
*/
public function onConfigLoaded(array &$config)
{
if (isset($config['default_language'])) {
$this->default_language = $config['default_language'];
}
// check languages
if (isset($config['languages'])) {
$this->languages = $config['languages'];
}
if (!is_array($this->languages)) {
throw new RuntimeException('Invalid languages, "' . $this->languages . '" must be an array');
}
$this->available_languages = array_keys($this->languages);
// check language dir
if (isset($config['language_dir'])) {
$this->language_dir = $config['language_dir'];
}
if (!is_dir($this->language_dir)) {
throw new RuntimeException('Invalid language directory "' . $this->language_dir . '"');
}
$this->language_dir = $this->getPico()->getAbsolutePath($this->language_dir);
// check site titles
if (isset($config['site_titles'])) {
$this->site_titles = $config['site_titles'];
}
if (!is_array($this->site_titles)) {
throw new RuntimeException('Invalid site titles, "' . $this->site_titles . '" must be an array');
}
// check date formats
if (isset($config['date_formats'])) {
$this->date_formats = $config['date_formats'];
}
if (!is_array($this->date_formats)) {
throw new RuntimeException('Invalid date formats, "' . $this->date_formats . '" must be an array');
}
// check date locales
if (isset($config['date_locales'])) {
$this->date_locales = $config['date_locales'];
}
if (!is_array($this->date_locales)) {
throw new RuntimeException('Invalid date locales, "' . $this->date_locales . '" must be an array');
}
}
/**
* Triggered after Pico has evaluated the request URL
*
* @see Pico::getRequestUrl()
*
* @param string &$url part of the URL describing the requested contents
*/
public function onRequestUrl(&$url)
{
$content_dir = $this->getPico()->getConfig('content_dir');
$content_ext = $this->getPico()->getConfig('content_ext');
// Checks if an index page exists at the root of the
// content_dir when the url is the base_url
if ($url == '' && !is_file($content_dir . '/index' . $content_ext)) {
// Redirects to the index page of the language
$language = $this->getBrowserLanguage();
if (is_file($content_dir . $language . '/index' . $content_ext)) {
$url = $language;
}
}
}
/**
* Triggered after Pico has read the contents of the 404 file
*
* @see DummyPlugin::on404ContentLoading()
* @see Pico::getRawContent()
* @see Pico::is404Content()
*
* @param string &$rawContent raw file contents
*/
public function on404ContentLoaded(&$rawContent)
{
$content_dir = $this->getPico()->getConfig('content_dir');
$content_dir_length = strlen($content_dir);
$content_ext = $this->getPico()->getConfig('content_ext');
$file = $this->getPico()->getRequestFile();
$file_length = strlen($file);
$file_path = explode('/', substr($file, $content_dir_length, $file_length));
// Checks if a 404 page exists at the root of the
// content_dir when the file path does not use a language
if (!in_array($file_path[0], $this->available_languages) && !is_file($content_dir . '/404' . $content_ext)) {
// Load the contents of the 404 file for the language
$language = $this->getBrowserLanguage();
if (is_file($content_dir . $language . '/404' . $content_ext)) {
$rawContent = $this->getPico()->loadFileContent($content_dir . $language . '/404' . $content_ext);
}
}
}
/**
* Triggered when Pico reads its known meta header fields
*
* @see Pico::getMetaHeaders()
*
* @param string[] &$headers list of known meta header fields; the array
* key specifies the YAML key to search for, the array value is later
* used to access the found value
*/
public function onMetaHeaders(array &$headers)
{
$headers['language'] = 'Language';
$headers['pid'] = 'pid';
}
/**
* Triggered after Pico has parsed the meta header
*
* @see DummyPlugin::onMetaParsing()
* @see Pico::getFileMeta()
*
* @param string[] &$meta parsed meta data
*/
public function onMetaParsed(array &$meta)
{
// Checks if the language of the page is set and that it is available for the site
if (!$meta['language'] || !in_array($meta['language'], $this->available_languages)) {
$meta['language'] = $this->default_language;
}
$this->current_language = $meta['language'];
}
/**
* Triggered after Pico has prepared the raw file contents for parsing
*
* @see DummyPlugin::onContentParsing()
* @see Pico::parseFileContent()
* @see DummyPlugin::onContentParsed()
*
* @param string &$markdown Markdown contents of the requested page
*/
public function onContentPrepared(&$markdown)
{
$variables = array();
// replace %language_base_url%
$variables['%language_base_url%'] = rtrim($this->getLanguageBaseUrl(), '/');
$markdown = str_replace(array_keys($variables), $variables, $markdown);
}
/**
* Triggered when Pico reads a single page from the list of all known pages
*
* The `$pageData` parameter consists of the following values:
*
* | Array key | Type | Description |
* | -------------- | ------ | ---------------------------------------- |
* | id | string | relative path to the content file |
* | url | string | URL to the page |
* | title | string | title of the page (YAML header) |
* | description | string | description of the page (YAML header) |
* | author | string | author of the page (YAML header) |
* | time | string | timestamp derived from the Date header |
* | date | string | date of the page (YAML header) |
* | date_formatted | string | formatted date of the page |
* | raw_content | string | raw, not yet parsed contents of the page |
* | meta | string | parsed meta data of the page |
*
* @see DummyPlugin::onSinglePageLoading()
* @see DummyPlugin::onSinglePageContent()
*
* @param array &$pageData data of the loaded page
*/
public function onSinglePageLoaded(array &$pageData)
{
$language = $pageData['meta']['language'] ?? $this->default_language;
$page_id = $pageData['meta']['pid'];
// set page.language, page.is_current_language and page.id
$pageData['language'] = $language;
$pageData['is_current_language'] = $language === $this->current_language;
$pageData['pid'] = $page_id;
// edit page.date_formatted
$pageData['date_formatted'] = $this->localizeAndFormatDate($pageData['date']);
// add page to languages[$lang]
if (!isset($this->pages_by_language[$language])) {
$this->pages_by_language[$language] = array();
}
$this->pages_by_language[$language][] = $pageData;
// add pages with Id to page_languages[$pid][$lang]
if ($page_id) {
if (!isset($this->pages_by_id[$page_id])) {
$this->pages_by_id[$page_id] = array();
}
$this->pages_by_id[$page_id][$language] = $pageData;
}
}
/**
* Triggered after Pico has sorted the pages array
*
* Please refer to {@see Pico::readPages()} for information about the
* structure of Pico's pages array and the structure of a single page's
* data.
*
* @see DummyPlugin::onPagesLoading()
* @see DummyPlugin::onPagesDiscovered()
* @see Pico::getPages()
*
* @param array[] &$pages sorted list of all known pages
*/
public function onPagesLoaded(array &$pages)
{
// only keep pages with same language as current
$pages = array_filter($pages, function ($page) {
return $page['is_current_language'];
});
}
/**
* Triggered before Pico renders the page
*
* @see DummyPlugin::onPageRendered()
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*/
public function onPageRendering(&$templateName, array &$twigVariables)
{
$twigVariables['languages'] = $this->languages;
$page_id = $twigVariables['meta']['pid'];
$twigVariables['page_languages'] = $this->pages_by_id[$page_id] ?? array();
if (isset($this->site_titles[$this->current_language]) && !empty($this->site_titles[$this->current_language])) {
$twigVariables['site_title'] = $this->site_titles[$this->current_language];
}
$twigVariables['current_language'] = $this->current_language;
$twigVariables['language_base_url'] = rtrim($this->getLanguageBaseUrl(), '/');
}
/**
* Returns the translated and formatted string for the key passed as a parameter.
* Search in the file corresponding to the current language in the language directory.
* (e.g. `language/en.php`)
*
* @see https://php.net/manual/en/function.sprintf.php
* @param string $key
* @param array $args
* @return string translated string
*/
public function translate($key)
{
$lang_array = include $this->language_dir . $this->current_language . '.php';
if (!isset($lang_array[$key]) || empty($lang_array[$key])) {
return $key;
}
$translated_value = $lang_array[$key];
$args = func_get_args();
if (count($args) > 1) {
// Replace $key by $translated_value in $args array
$args[0] = $translated_value;
// Format the string
$translated_value = call_user_func_array("sprintf", $args);
}
return $translated_value;
}
/**
* Registers Twig filters.
*/
public function onTwigRegistration()
{
$this->getPico()->getTwig()->addFilter(new \Twig\TwigFilter('translate', [$this, 'translate']));
}
/**
* Returns the browser language if available in the languages array, or the default language.
*
* @return string
*/
private function getBrowserLanguage()
{
$browser_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
return in_array($browser_language, $this->available_languages) ? $browser_language : $this->default_language;
}
/**
* Returns the Base URL for the current language.
* Is equivalent to `{{ base_url }}/{{ current_language }}` and
* `{{ base_url }}?{{ current_language }}`, depending on enabled URL rewriting.
*
* @return string the language_base_url
*/
private function getLanguageBaseUrl()
{
return $this->getPico()->getBaseUrl()
. (!$this->getPico()->isUrlRewritingEnabled() ? '?' : '')
. $this->current_language;
}
/**
* Returns the localized and formatted date.
*
* @see https://php.net/manual/en/function.strftime.php
* @param string $date
* @return string date formatted and localized
*/
private function localizeAndFormatDate($date)
{
setlocale(LC_TIME, ''); // reset the locale
$locale = $this->date_locales[$this->current_language] ?? $this->current_language;
setlocale(LC_TIME, $locale); // set the current locale, stay on the default value if it does not exist
$date_format = $this->date_formats[$this->current_language] ?? $this->getPico()->getConfig('date_format');
$formatted_date = utf8_encode(strftime($date_format, strtotime($date)));
setlocale(LC_TIME, ''); // reset the locale
return $formatted_date;
}
}