-
Notifications
You must be signed in to change notification settings - Fork 0
/
RockLanguage.module.php
149 lines (135 loc) · 4.07 KB
/
RockLanguage.module.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
<?php
namespace ProcessWire;
/**
* @author Bernhard Baumrock, 05.06.2022
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
class RockLanguage extends WireData implements Module, ConfigurableModule
{
public static function getModuleInfo()
{
return [
'title' => 'RockLanguage',
'version' => '1.0.1',
'summary' => 'Easily manage and ship translation files for your modules',
'autoload' => true,
'singular' => true,
'icon' => 'language',
];
}
public function init()
{
$this->sync();
}
/**
* Copy file if it is newer
*/
public function copyIfNewer($from, $to, $toName)
{
if (is_file($to) and filemtime($from) <= filemtime($to)) return;
$this->wire->files->copy($from, $to);
$fromName = basename($from);
$this->log("Copied $fromName to $toName");
}
public function getCustomCodes()
{
$codes = [];
foreach (explode("\n", $this->customCodes ?: '') as $line) {
$parts = explode("=", $line, 2);
if (count($parts) != 2) continue;
$codes[$parts[0]] = $parts[1];
}
return $codes;
}
/**
* @return array
*/
public function getLanguageCodes($returnString = false)
{
if (!$this->wire->languages) return [];
$codes = [];
$string = '';
$custom = $this->getCustomCodes();
foreach ($this->wire->languages as $lang) {
$code = strtoupper($lang->name);
if (array_key_exists($lang->name, $custom)) $code = $custom[$lang->name];
$codes[$lang->id] = $code;
$string .= "$lang->name=$code<br>";
}
if ($returnString) return $string;
return $codes;
}
/**
* pull translation files from modules to language
*/
public function pull()
{
foreach ($this->getLanguageCodes() as $id => $code) {
// find all language files in site modules
$modules = $this->wire->config->paths->siteModules;
$files = glob($modules . "*/RockLanguage/$code/site--modules--*.json");
foreach ($files as $moduleFile) {
$name = basename($moduleFile);
$langFile = $this->wire->config->paths->files . $id . "/$name";
$this->copyIfNewer($moduleFile, $langFile, "language $id");
}
}
}
/**
* push translation files from language to modules
*/
public function push()
{
foreach ($this->getLanguageCodes() as $id => $code) {
// find all language files in language folder
$langpath = $this->wire->config->paths->files . $id . "/";
$files = glob($langpath . "site--modules--*.json");
foreach ($files as $langFile) {
$json = json_decode(file_get_contents($langFile));
$parts = explode("/", $json->file);
$module = $parts[2];
$dir =
$this->wire->config->paths->siteModules
. "$module/RockLanguage/$code/";
if (!is_dir($dir)) continue;
$moduleFile = $dir . basename($langFile);
$this->copyIfNewer($langFile, $moduleFile, "module $module");
}
}
}
/**
* Sync language files
*/
public function sync()
{
if (!$this->wire->user->isSuperuser()) return;
if (!$this->wire->config->debug) return;
$this->pull();
$this->push();
}
/**
* Config inputfields
* @param InputfieldWrapper $inputfields
*/
public function getModuleConfigInputfields($inputfields)
{
$inputfields->add([
'type' => 'markup',
'label' => 'Available Mappings',
'value' => $this->getLanguageCodes(true),
'notes' => 'This field lists all available languages and their folder names that will be used to push/pull translation files to/from.
A foldername **DE** means that translations will be synced to /site/modules/YourModule/RockLanguage/**DE**/yourfile.json;
Sync will only take place if $config->debug=true and user is superuser!',
]);
$inputfields->add([
'type' => 'textarea',
'name' => 'customCodes',
'label' => 'Language Code Mapping Overrides',
'notes' => 'Enter one per line. Also see notes in the field above.
Example: default=DE',
'value' => $this->customCodes,
]);
return $inputfields;
}
}