Last active
February 13, 2024 14:21
-
-
Save Dimitris1990/0aabe4cabd0d12e0fbffccac1536ae9e to your computer and use it in GitHub Desktop.
Convert .po files from Drupal7 to .yml for Drupal9
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 | |
/** | |
* First enable translation tracking (if it's disabled) by removing `collections.language.*` | |
* from /admin/config/development/configuration/ignore. | |
* After that, export all field translation .po files from the drupal7 site and save them | |
* in a directory in your project. | |
* The way the script is made requires some specifications: | |
* -The dir that the .po files are stored (e.g. /path/to/poFiles) | |
* -The dir that contains the language files (e.g. /path/to/config/sync/languages/) | |
* -The entities you want the translations from | |
* -The field machine names that are translated | |
*/ | |
// Specify the path to your project directory containing .po files. | |
$po_directory = ''; | |
// Specify the output directory for .yml files. | |
$output_directory = ''; | |
// Specify the entity names to search for translations. | |
$translation_entity = ['']; | |
// Specify the field machine names to search for translations. | |
$translation_fields = ['']; | |
// Scan the project directory for .po files. | |
$po_files = glob($po_directory . '/*.po'); | |
// Process each .po file. | |
foreach ($po_files as $po_file) { | |
echo "Processing $po_file...\n"; | |
// Initialize an array to store into key-value pairs the .po file contents. | |
$yaml_data = []; | |
// Extract the language code from the .po file name (e.g., "fr" from "fr.po"). | |
$language_code = pathinfo($po_file, PATHINFO_FILENAME); | |
// Determine the dynamic output directory based on the language code. | |
$dynamic_output_directory = $output_directory . $language_code . '/'; | |
// Create the dynamic output directory if it doesn't exist. | |
// if (!file_exists($dynamic_output_directory)) { | |
// mkdir($dynamic_output_directory, 0755, true); | |
// } | |
// Read the content of the .po file. | |
$po_content = file_get_contents($po_file); | |
// Split the content into individual entries. | |
$entries = explode("\n\n", $po_content); | |
foreach ($entries as $entry) { | |
// Split the entry into lines. | |
$lines = explode("\n", $entry); | |
$entry_array = [ | |
'entity' => '', | |
'msgctxt' => '', | |
'msgid' => '', | |
'msgstr' => '', | |
]; | |
foreach ($lines as $line) { | |
// Extract values from msgctxt, msgid, and msgstr lines. | |
if (preg_match('/^msgctxt "(.+)"$/', $line, $matches)) { | |
// Extract the first part of msgctxt. | |
$msgctxt_parts = explode(':', $matches[1]); | |
// Extract the machine name for msgctxt. | |
$field_machine_name = isset($msgctxt_parts[0]) ? $msgctxt_parts[0] : ''; | |
// Extract the entity type. | |
$entity = isset($msgctxt_parts[1]) ? $msgctxt_parts[1] : ''; | |
// Insert in the array the extracted values. | |
$entry_array['entity'] = $entity; | |
$entry_array['msgctxt'] = $field_machine_name; | |
} elseif (preg_match('/^msgid "(.+)"$/', $line, $matches)) { | |
$entry_array['msgid'] = $matches[1]; | |
} elseif (preg_match('/^msgstr "(.+)"$/', $line, $matches)) { | |
$entry_array['msgstr'] = $matches[1]; | |
} | |
} | |
// Check if both entity and msgctxt are in the specified arrays. | |
if (!in_array($entry_array['entity'], $translation_entity) || | |
!in_array($entry_array['msgctxt'], $translation_fields) || | |
empty($entry_array['msgstr'])) { | |
// Skip this entry if it doesn't match the specified values. | |
continue; | |
} | |
// Add the entry to the yaml_data array. | |
$yaml_data[] = $entry_array; | |
} | |
foreach ($yaml_data as $key => $value) { | |
// Build the dynamic .yml file name. | |
// For now it only created node field translations. In the future the "field.field.node" | |
// part will be dynamic. | |
$yml_file_name = "field.field.node.{$value['entity']}.{$value['msgctxt']}.yml"; | |
// Build the contents of the .yml file. | |
$yml_contents = "label: \"{$value['msgstr']}\"\ndescription: ''"; | |
// Save the contents to the dynamic .yml file. | |
$output_file_path = $dynamic_output_directory . $yml_file_name; | |
// Create the file in the appropriate dir and fill it. | |
file_put_contents($output_file_path, $yml_contents); | |
echo "Saved $yml_file_name file to: $dynamic_output_directory\n"; | |
} | |
echo "Finished processing $po_file.\n\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment