Created
March 14, 2018 14:31
-
-
Save ruby232/35eb72a1ebf4b03409aac510e76d32b4 to your computer and use it in GitHub Desktop.
Import Translations in Drupal 8 programmatically.
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
importTranslations([drupal_get_path('profile', 'myprofile')."/translations/es.po"]); |
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
function importTranslations( array $poFiles) { | |
\Drupal::moduleHandler()->loadInclude('locale', 'translation.inc'); | |
\Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc'); | |
$opt_langcode = 'es'; | |
$opt_set_customized = TRUE; | |
$opt_replace_customized = TRUE; | |
$opt_replace_not_customized = TRUE; | |
if (!$poFiles) { | |
if ($dir = Settings::get('custom_translations_directory')) { | |
$poFiles = glob(DRUPAL_ROOT . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . '*.po'); | |
} | |
else { | |
\Drupal::logger('update') | |
->info(t('Nothing to do, no file given and no custom translation directory set.')); | |
} | |
} | |
$importer_options = array_merge(_locale_translation_default_update_options(), [ | |
'langcode' => $opt_langcode, | |
'customized' => $opt_set_customized ? LOCALE_CUSTOMIZED : LOCALE_NOT_CUSTOMIZED, | |
'overwrite_options' => [ | |
'customized' => (int) $opt_replace_customized, | |
'not_customized' => (int) $opt_replace_not_customized, | |
], | |
]); | |
// Import language files. | |
$files = []; | |
$langcodes_to_import = []; | |
foreach ($poFiles as $file_path) { | |
// Ensure we have the file intended for upload. | |
if (file_exists($file_path)) { | |
$file = locale_translate_file_create($file_path); | |
// Extract project, version and language code from the file name | |
// Supported: | |
// - {project}-{version}.{langcode}.po, {prefix}.{langcode}.po | |
// - {langcode}.po | |
// Note: $options['langcode'] will override file langcode. | |
$file = locale_translate_file_attach_properties($file, $importer_options); | |
if ($file->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) { | |
if (!$opt_langcode) { | |
\Drupal::logger('update') | |
->error(t('Can not autodetect language of file @file', ['@file' => $file_path])); | |
return; | |
} | |
$file->langcode = $opt_langcode; | |
if (empty($file->version) && !empty($file->project) && !empty($file->langcode)) { | |
$sources = locale_translation_get_status(); | |
$source = $sources[$file->project][$file->langcode]; | |
if (isset($source->version)) { | |
$file->version = $source->version; | |
} | |
} | |
} | |
$langcodes_to_import[$file->langcode] = $file->langcode; | |
$files[] = $file; | |
} | |
else { | |
\Drupal::logger('update') | |
->error(t('File to import at @filepath not found.', ['@filepath' => $file_path])); | |
} | |
} | |
$batch = locale_translate_batch_build($files, $importer_options); | |
batch_set($batch); | |
// Create or update all configuration translations for this language. | |
if ($batch = locale_config_batch_update_components($importer_options, $langcodes_to_import)) { | |
batch_set($batch); | |
} | |
\Drupal::logger('update')->info(t('Import complete.')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment