36 $this->codes = $codes;
47 $dir =
Paths::sanitize($dir, false);
49 if (is_dir(
"{$dir}/install/languages")) {
53 if (is_dir(
"{$dir}/languages")) {
57 $mods = new \DirectoryIterator(
"{$dir}/mod");
59 foreach ($mods as $mod) {
60 if ($mod->isDot() || !$mod->isDir()) {
69 if (is_dir(
"{$mod->getPathname()}/languages")) {
84 $dir =
Paths::sanitize($dir, false);
86 $files = new \DirectoryIterator($dir);
87 foreach ($files as $file) {
88 if ($file->isDot() || !$file->isFile()) {
92 if ($file->getExtension() !==
'php') {
96 $code = $file->getBasename(
'.php');
97 if (!in_array(
$code, $this->codes)) {
100 if (in_array(
$code, $this->codes)) {
102 rename($file->getPathname(),
"{$dir}/{$code}.php");
103 $this->log[] =
"Renamed {$file->getPathname()} to {$code}.php";
105 unlink($file->getPathname());
106 $this->log[] =
"Removed {$file->getPathname()}";
110 if (
$code !==
'en' && file_exists(
"{$dir}/{$code}.php")) {
113 $this->cleanupMissingTranslationParameters($dir,
$code);
114 $this->cleanupEmptyTranslations(
"{$dir}/{$code}.php");
115 $this->cleanupTranslationsThreshold($dir,
$code);
129 $english =
Includer::includeFile(
"{$directory}/en.php");
130 $translation = Includer::includeFile(
"{$directory}/{$language_code}.php");
133 $english_matches = preg_match_all(
'/%[a-zA-Z]/m',
$value);
134 if (!array_key_exists(
$key, $translation) || $english_matches ===
false) {
138 $translation_matches = preg_match_all(
'/%[a-zA-Z]/m', $translation[
$key]);
139 if ($translation_matches !==
false && $english_matches === $translation_matches) {
143 $file_contents = file_get_contents(
"{$directory}/{$language_code}.php");
145 $pattern =
'/^\s*[\'"]' .
$key .
'[\'"] => [\'"]' . preg_quote($translation[
$key],
'/') .
'[\'"],{0,1}\R/m';
150 $pattern =
'/^\s*[\'"]' .
$key .
'[\'"] => [\'"]' . preg_quote(addslashes($translation[
$key]),
'/') .
'[\'"],{0,1}\R/m';
156 file_put_contents(
"{$directory}/{$language_code}.php",
$file_contents);
158 $this->
log[] =
"Unable to repair mismatch in translation argument count in {$directory}/{$language_code}.php for the key '{$key}'";
171 $contents = file_get_contents($translation_file);
176 $pattern =
'/^\s*[\'"].*[\'"] => [\'"]{2},{0,1}\R/m';
182 file_put_contents($translation_file,
$contents);
186 $this->log[] =
"Cleaned empty translations from {$translation_file}";
188 unlink($translation_file);
190 $this->log[] =
"Removed empty translation file {$translation_file}";
205 if (!file_exists(
"{$directory}/{$language_code}.php")) {
210 $english = Includer::includeFile(
"{$directory}/en.php");
211 $translation = Includer::includeFile(
"{$directory}/{$language_code}.php");
213 if (empty($translation)) {
214 unlink(
"{$directory}/{$language_code}.php");
216 $this->log[] =
"Removed empty translation file {$directory}/{$language_code}.php";
220 $trans_percentage = (count($translation) / count($english)) * 100;
221 if ($trans_percentage < $min_percentage) {
222 unlink(
"{$directory}/{$language_code}.php");
223 $trans_percentage = round($trans_percentage, 2);
225 $this->log[] =
"Removed translation file {$directory}/{$language_code}.php below minimal threshold ({$trans_percentage} < {$min_percentage})";
238 return preg_replace(
'~[^a-z0-9]~',
'_',
$code);
250 $english =
Includer::includeFile(
"{$directory}/en.php");
251 $translation = Includer::includeFile(
"{$directory}/{$language_code}.php");
254 if (array_key_exists(
$key, $english)) {
258 $this->log[] =
"The translation key '{$key}' exists in the '{$language_code}' translation but not in English";
271 $english =
Includer::includeFile(
"{$directory}/en.php");
272 $translation = Includer::includeFile(
"{$directory}/{$language_code}.php");
275 if (!array_key_exists(
$key, $english)) {
280 if (strlen(
$key) < 3) {
289 $this->log[] =
"The translation key '{$key}' in the '{$language_code}' translation is identical to the English translation";
log($level, $message, array $context=[])
Log a message.
Persistent, installation-wide key-value storage.
Removes invalid language files from an installation.
__construct(array $codes=[])
Constructor.
cleanupMissingTranslationParameters(string $directory, string $language_code)
Try to cleanup translations with a different argument count than English as this can cause failed tra...
detectIdenticalTranslations(string $directory, string $language_code)
Detect identical translations, this could be due to a wrong Transifex import.
cleanLanguagesDir(string $dir)
Clean up a languages dir.
cleanInstallation(string $dir)
Clean up within an installation.
cleanupEmptyTranslations(string $translation_file)
Remove empty translations from a translation file.
cleanupTranslationsThreshold(string $directory, string $language_code, int $min_percentage=50)
Remove translation files with translations below a threshold.
detectAdditionalKeys(string $directory, string $language_code)
Detect translation keys that (still) exist in a translation but no longer in the English translation.
normalizeLanguageCode(string $code)
Normalize a language code (e.g.
Allow executing scripts without $this context or local vars.
Find Elgg and project paths.
_elgg_services()
Get the global service provider.
foreach(array_keys($combine_languages) as $language) $translations
if(!empty($title) &&!empty($icon_name)) if(!empty($title)) if(!empty($menu)) if(!empty($header)) if(!empty($body)) $contents
if(! $plugin instanceof \ElggPlugin) $file_contents
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.