Last active
May 13, 2022 14:48
-
-
Save denchev/4eca5f8e8ccffb9f27048d8a4171ee8d to your computer and use it in GitHub Desktop.
Scan a folder of a Salesforce Commerce Cloud project for .properties files and then moves each line to a CSV by preserving the path. Used for translations if you have to give it to an agency.
This file contains hidden or 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 | |
$root = realpath('./'); | |
$cartridges = []; | |
$countries = ['bg', 'sl', 'et', 'hu', 'nl', 'sv', 'da', 'de', 'pt', 'fi', 'it', 'ja', 'ro', 'ko', 'lt', 'pl', 'lv', 'lb', 'ca', 'es', 'fr', 'hr', 'ro', 'ru', 'sk', 'el', 'cs', 'zh', 'no', 'tr']; | |
$locales = ['nl_BE', 'de_DE', 'ja_JP', 'en_JP', 'en_DE', 'en_PH', 'en_US', 'en_HU', 'fr_FR', 'fr_IT', 'it_IT', 'zh_CN', 'en_GB']; | |
function findLanguageProperties($start, $cartridges) { | |
global $countries, $locales; | |
$handle = opendir($start); | |
$properties = []; | |
while(false !== ($entry = readdir($handle))) { | |
$fullPath = $start . DIRECTORY_SEPARATOR . $entry; | |
// Skip certain folders | |
if ($entry == '.' || $entry == '..' || $entry == '.git' || $entry === 'node_modules') { | |
continue; | |
} | |
if ($entry === 'cartridge') { | |
$prevDir = explode(DIRECTORY_SEPARATOR, $start); | |
$cartridgeName = end($prevDir); | |
// Do not use this cartridge | |
if (!in_array($cartridgeName, $cartridges)) { | |
continue; | |
} | |
} | |
if (is_dir($fullPath)) { | |
$properties = array_merge($properties, findLanguageProperties($fullPath, $cartridges)); | |
} else { | |
if (strpos($entry, '.properties') !== false) { | |
if (strpos($fullPath, 'templates/resources') !== false) { | |
$filename = basename($fullPath); | |
list($filename, $ext) = explode('.', $filename); | |
$filenameParts = explode('_', $filename); | |
if (count($filenameParts) === 1) { | |
$properties[] = $fullPath; | |
} else { | |
$country = end($filenameParts); | |
$locale = $filenameParts[count($filenameParts)-2] . '_' . $filenameParts[count($filenameParts)-1]; | |
if (!in_array($country, $countries) && !in_array($locale, $locales)) { | |
$properties[] = $fullPath; | |
} | |
} | |
} | |
} | |
} | |
} | |
closedir($handle); | |
return $properties; | |
} | |
function toRelativePath($fullPropertyPath) { | |
global $root; | |
return str_replace($root, '', $fullPropertyPath); | |
} | |
function propertiesToCSV($properties): array { | |
$masterCSV = []; | |
foreach($properties as $property) { | |
$handle = fopen($property, 'r'); | |
while(($line = fgets($handle, 4096)) !== false) { | |
// Skip line separators | |
if (strpos($line, '#') === 0) { | |
continue; | |
} | |
if(empty(trim($line))) { | |
continue; | |
} | |
list($key, $value) = explode('=', $line, 2); | |
if (trim($key) != '') { | |
$masterCSV[] = [$key, trim($value), toRelativePath($property)]; | |
} | |
} | |
} | |
return $masterCSV; | |
} | |
$properties = findLanguageProperties($root, $cartridges); | |
$csvContent = propertiesToCSV($properties); | |
$output = fopen('translations.csv', 'w'); | |
foreach($csvContent as $fields) { | |
fputcsv($output, $fields); | |
} | |
fclose($output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment