class i18n_string_object_wrapper in Internationalization 7
String object wrapper
Hierarchy
- class \i18n_object_wrapper
- class \i18n_string_object_wrapper
Expanded class hierarchy of i18n_string_object_wrapper
1 string reference to 'i18n_string_object_wrapper'
- i18n_string_i18n_object_info_alter in i18n_string/
i18n_string.module - Implements hook_i18n_object_info_alter().
File
- i18n_string/
i18n_string.inc, line 1110 - API for internationalization strings
View source
class i18n_string_object_wrapper extends i18n_object_wrapper {
// Text group object
protected $textgroup;
// Properties for translation
protected $properties;
/**
* Get object strings for translation
*
* This will return a simple array of string objects, indexed by full string name.
*
* @param $options
* Array with processing options.
* - 'empty', whether to return empty strings, defaults to FALSE.
*/
public function get_strings($options = array()) {
$options += array(
'empty' => FALSE,
);
$strings = array();
foreach ($this
->get_properties() as $textgroup => $textgroup_list) {
foreach ($textgroup_list as $type => $type_list) {
foreach ($type_list as $object_id => $object_list) {
foreach ($object_list as $key => $string) {
if ($options['empty'] || !empty($string['string'])) {
// Build string object, that will trigger static caches everywhere.
$i18nstring = i18n_string_textgroup($textgroup)
->build_string(array(
$type,
$object_id,
$key,
))
->set_string($string);
$strings[$i18nstring
->get_name()] = $i18nstring;
}
}
}
}
}
return $strings;
}
/**
* Get object translatable properties
*
* This will return a big array indexed by textgroup, object type, object id and string key.
* Each element is an array with string information, and may have these properties:
* - 'string', the string itself, will be NULL if the object doesn't have that string
* - 'format', string format when needed
* - 'title', string readable name
*/
public function get_properties() {
if (!isset($this->properties)) {
$this->properties = $this
->build_properties();
// Call hook_i18n_string_list_TEXTGROUP_alter(), last chance for modules
drupal_alter('i18n_string_list_' . $this
->get_textgroup(), $this->properties, $this->type, $this->object);
}
return $this->properties;
}
/**
* Build properties from object.
*/
protected function build_properties() {
list($string_type, $object_id) = $this
->get_string_context();
$object_keys = array(
$this
->get_textgroup(),
$string_type,
$object_id,
);
$strings = array();
foreach ($this
->get_string_info('properties', array()) as $field => $info) {
$info = is_array($info) ? $info : array(
'title' => $info,
);
$field_name = isset($info['field']) ? $info['field'] : $field;
$value = $this
->get_field($field_name);
if (is_array($value) && isset($value['value'])) {
$format = isset($value['format']) ? $value['format'] : NULL;
$value = $value['value'];
}
else {
$format = isset($info['format']) ? $this
->get_field($info['format']) : NULL;
}
$strings[$this
->get_textgroup()][$string_type][$object_id][$field] = array(
'string' => is_array($value) || isset($info['empty']) && $value === $info['empty'] ? NULL : $value,
'title' => $info['title'],
'format' => $format,
'name' => array_merge($object_keys, array(
$field,
)),
);
}
return $strings;
}
/**
* Get string context
*/
public function get_string_context() {
return array(
$this
->get_string_info('type'),
$this
->get_key(),
);
}
/**
* Get translate path for object
*
* @param $langcode
* Language code if we want ti for a specific language
*/
public function get_translate_path($langcode = NULL) {
$replacements = array(
'%i18n_language' => $langcode ? $langcode : '',
);
if ($path = $this
->get_string_info('translate path')) {
return $this
->path_replace($path, $replacements);
}
elseif ($path = $this
->get_info('translate tab')) {
// If we've got a translate tab path, we just add language to it
return $this
->path_replace($path . '/%i18n_language', $replacements);
}
}
/**
* Translation mode for object
*/
public function get_translate_mode() {
return !$this
->get_langcode() ? I18N_MODE_LOCALIZE : I18N_MODE_NONE;
}
/**
* Get textgroup name
*/
public function get_textgroup() {
return $this
->get_string_info('textgroup');
}
/**
* Get textgroup object
*/
protected function textgroup() {
if (!isset($this->textgroup)) {
$this->textgroup = i18n_string_textgroup($this
->get_textgroup());
}
return $this->textgroup;
}
/**
* Translate object.
*
* Translations are cached so it runs only once per language.
*
* @return object/array
* A clone of the object with its properties translated.
*/
public function translate($langcode, $options = array()) {
// We may have it already translated. As objects are statically cached, translations are too.
if (!isset($this->translations[$langcode])) {
$this->translations[$langcode] = $this
->translate_object($langcode, $options);
}
return $this->translations[$langcode];
}
/**
* Translate access (localize strings)
*/
protected function localize_access() {
// We could check also whether the object has strings to translate:
// && $this->get_strings(array('empty' => TRUE))
// However it may be better to display the 'No available strings' message
// for the user to have a clue of what's going on. See i18n_string_translate_page_object()
return user_access('translate interface') && user_access('translate user-defined strings');
}
/**
* Translate all properties for object.
*
* On top of object strings we search for all textgroup:type:objectid:* properties
*
* @param $langcode
* A clone of the object or array
*/
protected function translate_object($langcode, $options) {
// Clone object or array so we don't affect the original one.
$object = is_object($this->object) ? clone $this->object : $this->object;
// Get object strings for translatable properties.
if ($strings = $this
->get_strings()) {
// We preload some of the property translations with a single query.
if ($context = $this
->get_translate_context($langcode, $options)) {
$found = $this
->textgroup()
->multiple_translation_search($context, $langcode);
}
// Replace all strings in object.
foreach ($strings as $i18nstring) {
$this
->translate_field($object, $i18nstring, $langcode, $options);
}
}
return $object;
}
/**
* Context to be pre-loaded before translation.
*/
protected function get_translate_context($langcode, $options) {
// One-query translation of all textgroup:type:objectid:* properties
$context = $this
->get_string_context();
$context[] = '*';
return $context;
}
/**
* Translate object property.
*
* Mot often, this is a direct field set, but sometimes fields may have different formats.
*/
protected function translate_field(&$object, $i18nstring, $langcode, $options) {
$field_name = $i18nstring->property;
$translation = $i18nstring
->format_translation($langcode, $options);
if (is_object($object)) {
$object->{$field_name} = $translation;
}
elseif (is_array($object)) {
$object[$field_name] = $translation;
}
}
/**
* Remove all strings for this object.
*/
public function strings_remove($options = array()) {
$result = array();
foreach ($this
->load_strings() as $key => $string) {
$result[$key] = $string
->remove($options);
}
return _i18n_string_result_count($result);
}
/**
* Update all strings for this object.
*/
public function strings_update($options = array()) {
$options += array(
'empty' => TRUE,
'update' => TRUE,
);
$result = array();
$existing = $this
->load_strings();
// Update object strings
foreach ($this
->get_strings($options) as $key => $string) {
$result[$key] = $string
->update($options);
unset($existing[$key]);
}
// Delete old existing strings.
foreach ($existing as $key => $string) {
$result[$key] = $string
->remove($options);
}
return _i18n_string_result_count($result);
}
/**
* Load all existing strings for this object.
*/
public function load_strings() {
list($type, $id) = $this
->get_string_context();
return $this
->textgroup()
->load_strings(array(
'type' => $type,
'objectid' => $id,
));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
i18n_object_wrapper:: |
protected | property | ||
i18n_object_wrapper:: |
protected | property | ||
i18n_object_wrapper:: |
protected | property | ||
i18n_object_wrapper:: |
protected | property | ||
i18n_object_wrapper:: |
function | Get edit path for object | 2 | |
i18n_object_wrapper:: |
function | Get field value from object/array | ||
i18n_object_wrapper:: |
function | Get string numeric key for indexing. | ||
i18n_object_wrapper:: |
public | function | Get object info | |
i18n_object_wrapper:: |
function | Get key value from object/array | ||
i18n_object_wrapper:: |
public | function | Get language code | |
i18n_object_wrapper:: |
public | function | Get real object or array. | |
i18n_object_wrapper:: |
public | function | Get link for item | 2 |
i18n_object_wrapper:: |
protected | function | Get menu placehoders for object | |
i18n_object_wrapper:: |
public | function | Get object string translation info | |
i18n_object_wrapper:: |
public | function | Get title from item | 2 |
i18n_object_wrapper:: |
function | Menu access callback for mixed translation tab | ||
i18n_object_wrapper:: |
public | function | Get object translation set info | |
i18n_object_wrapper:: |
function | Get translation set id if any | ||
i18n_object_wrapper:: |
public | function | Get object type | |
i18n_object_wrapper:: |
function | Load real object or array. | 1 | |
i18n_object_wrapper:: |
function | Localize object if localizable. | ||
i18n_object_wrapper:: |
protected | function | Replace path with placeholders | |
i18n_object_wrapper:: |
function | Set field value to object/array | ||
i18n_object_wrapper:: |
function | Set translation set id | ||
i18n_object_wrapper:: |
protected | function | Translate access (translation sets) | 2 |
i18n_object_wrapper:: |
public | function | Class constructor | 2 |
i18n_string_object_wrapper:: |
protected | property | ||
i18n_string_object_wrapper:: |
protected | property | ||
i18n_string_object_wrapper:: |
protected | function | Build properties from object. | 4 |
i18n_string_object_wrapper:: |
public | function | Get object translatable properties | |
i18n_string_object_wrapper:: |
public | function | Get object strings for translation | |
i18n_string_object_wrapper:: |
public | function | Get string context | 3 |
i18n_string_object_wrapper:: |
public | function | Get textgroup name | |
i18n_string_object_wrapper:: |
protected | function | Context to be pre-loaded before translation. | 2 |
i18n_string_object_wrapper:: |
public | function |
Translation mode for object Overrides i18n_object_wrapper:: |
3 |
i18n_string_object_wrapper:: |
public | function | Get translate path for object | 2 |
i18n_string_object_wrapper:: |
public | function | Load all existing strings for this object. | |
i18n_string_object_wrapper:: |
protected | function |
Translate access (localize strings) Overrides i18n_object_wrapper:: |
|
i18n_string_object_wrapper:: |
public | function | Remove all strings for this object. | |
i18n_string_object_wrapper:: |
public | function | Update all strings for this object. | |
i18n_string_object_wrapper:: |
protected | function | Get textgroup object | |
i18n_string_object_wrapper:: |
public | function |
Translate object. Overrides i18n_object_wrapper:: |
|
i18n_string_object_wrapper:: |
protected | function | Translate object property. | 2 |
i18n_string_object_wrapper:: |
protected | function | Translate all properties for object. |