Skip to content

Instantly share code, notes, and snippets.

@Quetzacoalt91
Created November 29, 2021 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quetzacoalt91/b5c25a2116c09dce30446da134f4c159 to your computer and use it in GitHub Desktop.
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
<?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