-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.php
59 lines (51 loc) · 1.91 KB
/
index.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
<?php
// Get the language, fallback to de if none is passed
$lang = isset( $_GET['lang'] ) ? $_GET['lang'] : 'de';
// Load the i18n file
if( !file_exists( __DIR__ . '/i18n/' . $lang . '/i18n.json' ) ) {
$lang = 'de';
}
$i18nDir = __DIR__ . '/i18n';
$i18nData = file_get_contents( $i18nDir . '/' . $lang . '/i18n.json' );
$i18nData = json_decode($i18nData, true);
// Get the url if passed
$url = isset( $_GET['url'] ) ? $_GET['url'] : '';
// Get the base html to output
$html = file_get_contents( __DIR__ . '/index.base.html' );
$html = str_replace( '{{i18n.lang}}', $lang, $html );
$html = str_replace( '{{url}}', htmlspecialchars( $url ), $html );
// If we have any i18n data replace the i18n codes with new strings
if ( $i18nData && isset( $i18nData['index'] ) ) {
foreach( $i18nData['index'] as $key => $string ) {
$html = str_replace( '{{i18n.index.' . $key . '}}', $string, $html );
}
}
// Iterate over the i18n subdirectories (ISO 639-1 codes) to construct a
// <li> element representation for the dropdown options
$langDirs = array_filter(
array_filter( glob( $i18nDir . '/*'), 'is_dir'),
function ( $langDir ) { return pathinfo($langDir)['basename'] !== 'uk'; }
);
$languageOptions = '';
foreach ( $langDirs as $langDir ) {
$isoLang = pathinfo($langDir)['basename'];
$languageOptions .= '<li><a href="?lang=' . $isoLang .'">' . $isoLang . '</a></li>';
}
// ... and insert that string into the template
$html = str_replace( '{{ languageOptions }}', $languageOptions, $html);
// Also replace html snippets
$htmlFiles = [
'about',
'feedback',
'legal',
'private-use',
];
foreach( $htmlFiles as $file ) {
$path = __DIR__ . '/i18n/' . $lang . '/' . $file . '.html';
if( !file_exists( $path ) ) {
$path = __DIR__ . '/i18n/de/' . $file . '.html';
}
$html = str_replace( '{{i18n.html.' . $file . '}}', file_get_contents( $path ), $html );
}
// Output the html
echo $html;