public function i18n_string_textgroup_default::multiple_translate in Internationalization 7
Translate array of source strings
Parameters
$context: Context array with placeholders (*)
$strings: Optional array of source strings indexed by the placeholder property
Return value
array Array of string objects (with translation) indexed by the placeholder field
File
- i18n_string/
i18n_string.inc, line 1031 - API for internationalization strings
Class
- i18n_string_textgroup_default
- Textgroup handler for i18n_string API
Code
public function multiple_translate($context, $strings = array(), $options = array()) {
// First, build conditions and identify the variable field
$search = $context = array_combine(array(
'type',
'objectid',
'property',
), $context);
$langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode();
// If we've got keyed source strings set the array of keys on the placeholder field
// or if not, remove that condition so we search all strings with that keys.
foreach ($search as $field => $value) {
if ($value === '*') {
$property = $field;
if ($strings) {
$search[$field] = array_keys($strings);
}
}
}
// Now we'll add the language code to conditions and get the translations indexed by the property field
$result = $this
->multiple_translation_search($search, $langcode);
// Remap translations using property field. If we've got strings it is important that they are in the same order.
$translations = $strings;
foreach ($result as $key => $i18nstring) {
$translations[$i18nstring->{$property}] = $i18nstring;
}
// Set strings as source or create
foreach ($strings as $key => $source) {
if (isset($translations[$key]) && is_object($translations[$key])) {
$translations[$key]
->set_string($source);
}
else {
// Not found any string for this property, create it to map in the response
// But make sure we set this language's translation to FALSE so we don't search again
$newcontext = $context;
$newcontext[$property] = $key;
$translations[$key] = $this
->build_string($newcontext)
->set_string($source)
->set_translation(FALSE, $langcode);
}
}
return $translations;
}