Skip to content

Instantly share code, notes, and snippets.

@opi
Last active April 28, 2020 18:27
Show Gist options
  • Save opi/5335785 to your computer and use it in GitHub Desktop.
Save opi/5335785 to your computer and use it in GitHub Desktop.
Drupal 7 : Add translation programmatically
<?php
$report = array(
'skips'=>0,
'updates'=>0,
'deletes'=>0,
'additions'=>0
); // ??
$source = "Sometime drupal sucks";
$translation = "Drupal C DLA BALLE";
$textgroup = 'default'; // field , node, default, menu ...
$langcode = 'fr'; // 'en', 'de'
$mode = LOCALE_IMPORT_OVERWRITE; // LOCALE_IMPORT_KEEP
$location = ''; // 'menu:menu:management:title', '/node/3', ...
_locale_import_one_string_db($report, $langcode, $context, $source, $translation, $textgroup, $location, $mode);
// Clear locale cache.
cache_clear_all('locale:', 'cache', TRUE);
<?php
// From https://fosswiki.liip.ch/display/DRUPAL/Script+creation+of+languages+and+import+po+files
/**
* Add translations from a PO file.
* This will populate the locales_target table in the database
* with the strings from the PO file.
*
* @param $langcode string (e.g. fr, de, it)
* @param $poFile string full path to .po file
* @param $mode const LOCALE_IMPORT_OVERWRITE (overwrite existing translations) or LOCALE_IMPORT_KEEP (only import new strings)
* @param $group string Text group to import PO file into (eg. 'default' for interface translations)
* @param $add_language_options array optional parameters for locale_add_language (name, native, direction, domain, prefix, enabled, default)
* @return bool success
*/
function project_update_load_language($langcode, $poFile, $mode=LOCALE_IMPORT_OVERWRITE, $group='default', $add_language_options=array()) {
$add_language_defaults = array(
'name' => NULL,
'native' => NULL,
'direction' => LANGUAGE_LTR,
'domain' => '',
'prefix' => '',
'enabled' => TRUE,
'default' => FALSE,
);
$options = array_merge($add_language_defaults, $add_language_options);
locale_add_language($langcode, $options['name'], $options['native'], $options['direction'],
$options['domain'], $options['prefix'], $options['enabled'], $options['default']);
// mimic a drupal uploaded file object, with the necessary info needed by _locale_import_po
$file = new stdClass;
$file->filepath = $poFile;
$file->filename = basename($poFile);
return _locale_import_po($file, $langcode, $mode, $group);
}
/**
* Find all po files under a given path
*
* @param $path string
* @param $recurse boolean
* @return array of string paths to po files
*/
function project_find_po_files($path, $recurse=TRUE) {
$path = rtrim($path, '/') . '/*';
$pattern = '/\.po$/';
$files = array();
foreach (glob ($path) as $fullname) {
if ($recurse && is_dir($fullname)) {
$files = array_merge($files, project_find_po_files($fullname, $recurse));
} else if (preg_match($pattern, $fullname)) {
$files[] = $fullname;
}
}
return $files;
}
@Renrhaf
Copy link

Renrhaf commented Jun 21, 2018

Thanks ! Agreed that Drupal 7 sucks at string translation API ;) But Drupal 8 fixed it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment