diff --git a/uc_attribute/uc_attribute.install b/uc_attribute/uc_attribute.install index 56736c8..a2f160e 100644 --- a/uc_attribute/uc_attribute.install +++ b/uc_attribute/uc_attribute.install @@ -466,7 +466,7 @@ function uc_attribute_update_6002() { $ret = array(); $result = db_query("SELECT * FROM {permission}"); - while($perm = db_fetch_object($result)) { + while ($perm = db_fetch_object($result)) { $perm->perm = str_replace("adminster attributes", "administer attributes", $perm->perm); drupal_write_record('permission', $perm, 'pid'); } @@ -505,7 +505,7 @@ function uc_attribute_update_6004() { $changes = 0; $result = db_query('SELECT * FROM {uc_product_adjustments}'); - while($row = db_fetch_object($result)) { + while ($row = db_fetch_object($result)) { $combination = unserialize($row->combination); ksort($combination); db_query("UPDATE {uc_product_adjustments} SET combination = '%s' WHERE nid = %d AND model = '%s' AND combination = '%s'", serialize($combination), $row->nid, $row->model, $row->combination); diff --git a/uc_attribute/uc_attribute.module b/uc_attribute/uc_attribute.module index 03daf9e..0664534 100644 --- a/uc_attribute/uc_attribute.module +++ b/uc_attribute/uc_attribute.module @@ -224,6 +224,94 @@ function uc_attribute_menu() { } /** + * Implementation of hook_locale(). + */ +function uc_attribute_locale($op = 'groups', $group = NULL) { + switch ($op) { + case 'groups': + return array('uc_attribute' => t('Übercart attribute')); + case 'info': + $info['uc_attribute']['refresh callback'] = 'uc_attribute_locale_refresh'; + $info['uc_attribute']['format'] = FALSE; + return $info; + } +} + +/** + * Implementation of hook_locale refresh callback. + */ +function uc_attribute_locale_refresh() { + // Checks if i18nstrings module is enabled + if (!function_exists('i18nstrings_update')) { + return FALSE; + } + + $attributes = db_query(" + SELECT uca.aid, + uca.name, + uca.label, + uca.description + FROM {uc_attributes} uca" + ); + + while ($attribute = db_fetch_object($attributes)) { + i18nstrings_update('uc_attribute:attribute:'. $attribute->aid .':name', $attribute->name); + i18nstrings_update('uc_attribute:attribute:'. $attribute->aid .':label', $attribute->label); + i18nstrings_update('uc_attribute:attribute:'. $attribute->aid .':description', $attribute->description); + + $options = db_query(" + SELECT ucao.oid, + ucao.name + FROM {uc_attribute_options} ucao + WHERE ucao.aid = %d", + array($attribute->aid) + ); + + while ($option = db_fetch_object($options)) { + i18nstrings_update('uc_attribute:option:'. $option->oid .':name', $option->name); + } + } + + return TRUE; // Meaning it completed with no issues +} + +/** + * Wrapper function for i18nstrings() if i18nstrings enabled. + * (Strongly inspired by this post http://drupal.org/node/609364 ) + * + * @param $name + * Name of the string, like "send_interval:$key:name" + * @param $string + * String in default language + * @param $langcode + * Optional language code to translate to, if not current page language + * @param $textgroup + * Text group name for the string. It defaults to 'uc_attribute' for this module + */ +function uc_attribute_translate($name, $string, $langcode = NULL) { + return function_exists('i18nstrings') ? i18nstrings('uc_attribute:' . $name, $string, $langcode) : $string; +} + +/** + * Translate an array of attributes and options as needed by the nodeapi + * hook. + * + * @param &$attributes + * An array + */ +function uc_attribute_object_translate($attribute) { + $attribute->name = uc_attribute_translate('attribute:'. $attribute->aid .':name', $attribute->name); + $attribute->label = uc_attribute_translate('attribute:'. $attribute->aid .':label', $attribute->label); + $attribute->description = uc_attribute_translate('attribute:'. $attribute->aid .':description', $attribute->description); + + foreach ($attribute->options as $oid => $option) { + $attribute->options[$oid]->name = uc_attribute_translate('option:'. $oid .':name', $option->name); + } + + return $attribute; +} + +/** * Access callback for editing a product class's attributes and options. */ function uc_attribute_product_class_access() { @@ -695,6 +783,13 @@ function uc_attribute_load($aid, $id = NULL, $type = '') { $attribute->label = $attribute->name; } + // Translate label and description if i18nstrings is installed + $attribute->label = uc_attribute_translate('attribute:'. $attribute->aid .':name', $attribute->label); + + if (!empty($attribute->description)) { + $attribute->description = uc_attribute_translate('attribute:'. $attribute->aid .':description', $attribute->description); + } + // Read option data. $result = db_query(" SELECT po.{$sql['id']}, po.oid, po.cost, po.price, po.weight, po.ordering, ao.name, @@ -723,6 +818,9 @@ function uc_attribute_load($aid, $id = NULL, $type = '') { // Get its options, too. $attribute->options = array(); while ($option = db_fetch_object($result)) { + // Translate option name if i18nstrings is installed + $option->name = uc_attribute_translate('option:'. $option->oid .'.:name', $option->name); + $attribute->options[$option->oid] = $option; } } @@ -1174,7 +1272,7 @@ function _uc_cart_product_get_options($item) { if (!empty($data['attributes']) && is_array($data['attributes'])) { foreach ($data['attributes'] as $aid => $selected) { if (isset($node->attributes[$aid])) { - $attribute = $node->attributes[$aid]; + $attribute = uc_attribute_object_translate($node->attributes[$aid]); $name = _uc_attribute_get_name($attribute); // Only discrete options can affect the price of an item. if ($attribute->display && count($attribute->options)) { @@ -1416,7 +1514,11 @@ function _uc_attribute_get_name($attribute, $title = TRUE) { if (!$title && $attribute->label == '') { return NULL; } + + if (empty($attribute->label) || ($attribute->label == '' && $title)) { + return uc_attribute_translate('attribute:'. $attribute->aid .':name', $attribute->name); + } else { - return (empty($attribute->label) || ($attribute->label == '' && $title) ? $attribute->name : $attribute->label); + return uc_attribute_translate('attribute:'. $attribute->aid .':label', $attribute->label); } }