forked from kimai/www.kimai.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-pages.php
79 lines (65 loc) · 3.07 KB
/
translate-pages.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
<?php
$languages = ['de', 'fr', 'hr'];
$multiLanguagePaths = [
'_pages' => [
'keep_front_matter' => ['title', 'description', 'lang', 'canonical', 'header', 'permalink', 'redirect_from'],
'skip' => ['v1.md'],
],
'_store' => [
'keep_front_matter' => ['title', 'description', 'lang', 'canonical', 'header', 'permalink', 'redirect_from', 'intro'],
'skip' => ['v1.md'],
'permalink' => '/:language/store/:basename',
],
// '_documentation' => [
// 'keep_front_matter' => ['title', 'description', 'redirect_from', 'canonical'],
// 'skip' => [],
// ],
];
foreach ($multiLanguagePaths as $path => $settings)
{
$basePath = __DIR__ . '/' . $path;
$files = glob($basePath . '/*.{md,html}', GLOB_BRACE);
$includePath = str_replace('_', '', $path);
$contentTarget = __DIR__ . '/_includes/' . $includePath;
$createPermalink = array_key_exists('permalink', $settings);
foreach ($files as $file) {
$baseFileName = basename($file);
foreach ($languages as $language) {
$langPath = $basePath . '/' . $language;
$langTargetFile = $langPath . '/' . $baseFileName;
if (in_array($baseFileName, $settings['skip'])) {
continue;
}
if (!file_exists($langTargetFile)) {
$newContentFile = $contentTarget . '/' . $baseFileName;
if (!file_exists($newContentFile)) {
$content = file_get_contents($file);
$pos = strpos($content, '---', strpos($content, '---') + 3);
$pureContent = substr($content, $pos + 3);
$include = $includePath . '/' . basename($file);
$newContent = substr($content, 0, $pos);
if (strpos($newContent, 'lang:') === false) {
$newContent .= 'lang: en' . PHP_EOL;
}
$newContent .= '---' . PHP_EOL . PHP_EOL . '{% include ' . $include . ' %}';
file_put_contents($file, $newContent);
file_put_contents($newContentFile, trim($pureContent) . PHP_EOL);
}
if (!file_exists($langPath)) {
mkdir($langPath, 0777, true);
}
copy($file, $langTargetFile);
$translatedContent = file_get_contents($langTargetFile);
$replaceMe = 'lang: ' . $language;
if ($createPermalink && strpos($translatedContent, 'permalink:') === false) {
$permalink = str_replace(':language', $language, $settings['permalink']);
$permalink = str_replace(':basename', basename($langTargetFile), $permalink);
$permalink = str_replace('.md', '.html', $permalink);
$replaceMe .= PHP_EOL . 'permalink: ' . $permalink;
}
$translatedContent = str_replace('lang: en', $replaceMe, $translatedContent);
file_put_contents($langTargetFile, $translatedContent);
}
}
}
}