cloudwords-8.x-1.x-dev/modules/cloudwords_interface_translation/cloudwords_interface_translation.module
modules/cloudwords_interface_translation/cloudwords_interface_translation.module
<?php
/**
* Implements hook_cloudwords_translatable_info().
*/
function cloudwords_interface_translation_cloudwords_translatable_info() {
$entity_controllers['interface'] = [
'controller class' => '\Drupal\cloudwords_interface_translation\CloudwordsInterfaceSourceController',
];
return $entity_controllers;
}
// @todo refresh translatables
/**
* Implements hook_cloudwords_refresh_translatables().
*/
function cloudwords_interface_translation_cloudwords_refresh_translatables(&$context){
// @todo exclude source language - ie en
$langcodes = array_keys(cloudwords_language_list());
$default_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$langcodes = array_diff($langcodes, [$default_language]);
$context['finished'] = 0;
$batch_limit = 50;
if (!isset($context['sandbox']['processed_runs'])) {
$context['sandbox']['processed_runs'] = 0;
}
if (!isset($context['sandbox']['translatable_items'])) {
$context['sandbox']['translatables_created'] = 0;
// @todo need translation module type field - replace these with translation bundle module type
$connection = \Drupal\Core\Database\Database::getConnection();
//translatables
$ct_data = $connection->select('cloudwords_translatable', 'ct')->fields('ct')->condition('ct.translation_module', CLOUDWORDS_INTERFACE_TRANSLATION_TYPE)->execute();
$ct_results = $ct_data->fetchAll(\PDO::FETCH_OBJ);
$translatables = [];
foreach ($ct_results as $ct) {
$translatables[$ct->objectid][$ct->language] = $ct;
}
$ls_data = $connection->select('locales_source', 'ls')->fields('ls')->execute();
$ls_results = $ls_data->fetchAll(\PDO::FETCH_OBJ);
foreach ($ls_results as $ls) {
foreach($langcodes as $langcode) {
if (!isset($translatables[$ls->lid][$langcode])){
$context['sandbox']['translatable_items'][$ls->lid]['langs'][$langcode] = $langcode;
}
}
if(!empty($context['sandbox']['translatable_items'][$ls->lid]['langs'])) {
$context['sandbox']['translatable_items'][$ls->lid]['lid'] = $ls->lid;
$context['sandbox']['translatable_items'][$ls->lid]['name'] = $ls->source;
}
}
}else{
$processed_in_batch = 0;
foreach($context['sandbox']['translatable_items'] as $translatable_item){
$context['message'] = 'Scanning interface translatable items. ' .count($context['sandbox']['translatable_items']).' remaining';
if(!empty($translatable_item['langs'])){
foreach($translatable_item['langs'] as $lang){
// add translatable
$lid = $translatable_item['lid'];
$new = cloudwords_translatable_create([
'translation_module' => CLOUDWORDS_INTERFACE_TRANSLATION_TYPE,
'objectid' => $lid,
'language' => $lang,
'name' => mb_strimwidth($translatable_item['name'], 0, 45),
'textgroup' => 'interface',
'type' => 'interface',
'bundle' => 'interface',
'user_id' => 0,
]);
$new->save();
$context['sandbox']['translatables_created'] += 1;
$processed_in_batch++;
unset($context['sandbox']['translatable_items'][$lid]['langs'][$lang]);
if(empty($context['sandbox']['translatable_items'][$lid]['langs'])){
unset($context['sandbox']['translatable_items'][$lid]);
}
}
}
unset($context['sandbox']['translatable_items'][$translatable_item['lid']]);
if ($processed_in_batch >= $batch_limit) {
break;
}
}
}
// @todo delete translatables where lids don't exist
if (empty($context['sandbox']['translatable_items'])) {
$msg = \Drupal::translation()->formatPlural($context['sandbox']['translatables_created'],
'Interface Translation: Created @count translatable',
'Interface Translation: Created @count translatables',
['%count' => $context['sandbox']['translatables_created']]);
drupal_set_message($msg);
$context['finished'] = 1;
}
// safeguard to prevent batch from running forever in the case of a data anomaly
$context['sandbox']['processed_runs'] += 1;
if($context['sandbox']['processed_runs'] == 1000){
$context['finished'] = 1;
}
}