Created
November 29, 2021 17:20
-
-
Save Quetzacoalt91/b5c25a2116c09dce30446da134f4c159 to your computer and use it in GitHub Desktop.
Using the English json file + the CSV with all the translations, generate one json file per lang we want to use on the module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// load strings | |
$allTranslations = []; | |
if (($handle = fopen("psxmarketingwithgoogle_19112021_bis.csv", "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) { | |
$key = $data['0']; | |
$allTranslations[$key] = $data; | |
} | |
fclose($handle); | |
} | |
// var_dump($allTranslations); | |
$enUiJson = json_decode(file_get_contents('en/ui.json'), true); | |
// var_dump($enUiJson); | |
function translate(array $sourceStrings, array $translationsFromAgency, $column) { | |
$translations = []; | |
foreach($sourceStrings as $key => $englishValue) { | |
if (is_array($englishValue)) { | |
$translations[$key] = translate($englishValue, $translationsFromAgency, $column); | |
} else { | |
// find in translations array | |
foreach ($translationsFromAgency as $reference => $translation) { | |
if ($reference === $englishValue) { | |
if (!empty($translation[$column])) { | |
$translations[$key] = $translation[$column]; | |
} | |
} | |
} | |
} | |
} | |
return $translations; | |
} | |
file_put_contents('it.json', json_encode(translate($enUiJson, $allTranslations, 1))); | |
file_put_contents('es.json', json_encode(translate($enUiJson, $allTranslations, 2))); | |
file_put_contents('de.json', json_encode(translate($enUiJson, $allTranslations, 3))); | |
file_put_contents('pt.json', json_encode(translate($enUiJson, $allTranslations, 4))); | |
file_put_contents('pl.json', json_encode(translate($enUiJson, $allTranslations, 5))); | |
file_put_contents('ru.json', json_encode(translate($enUiJson, $allTranslations, 6))); | |
file_put_contents('nl.json', json_encode(translate($enUiJson, $allTranslations, 7))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment