commit e33c3af552fb0d782ea1209fa1efdcc60507481c Author: Damien Tournoud Date: Tue Jul 5 18:33:58 2011 +0200 Issue #1121722: integrate with i18n_field for translation of the field instances. diff --git a/commerce.module b/commerce.module index cdaf8c8..17ae2cd 100644 --- a/commerce.module +++ b/commerce.module @@ -66,6 +66,25 @@ function commerce_info_fields($field_type, $entity_type = NULL) { } /** + * Translate an object. + * + * This is a loose integration point with i18n. Commerce will leverage i18n + * if available, or will just fallback to non translation. + */ +function commerce_object_translate($object_type, &$object, $langcode = NULL) { + if (!isset($langcode)) { + global $language; + $langcode = isset($language->language) ? $language->language : 'en'; + } + if (is_object($object)) { + $object = clone $object; + } + if (module_exists('i18n')) { + $object = i18n_object($object_type, $object)->translate($langcode); + } +} + +/** * Deletes a reference to another entity from an entity with a reference field. * * @param $entity_type diff --git a/modules/customer/commerce_customer.module b/modules/customer/commerce_customer.module index 3fbd3dd..d0b609b 100644 --- a/modules/customer/commerce_customer.module +++ b/modules/customer/commerce_customer.module @@ -612,6 +612,9 @@ function commerce_customer_field_settings_form($field, $instance, $has_data) { * line item ID). */ function commerce_customer_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); + if ($field['type'] == 'commerce_customer_profile_reference') { // Extract profile_ids to check. $profile_ids = array(); diff --git a/modules/customer/includes/commerce_customer.checkout_pane.inc b/modules/customer/includes/commerce_customer.checkout_pane.inc index 56f0819..e5479bb 100644 --- a/modules/customer/includes/commerce_customer.checkout_pane.inc +++ b/modules/customer/includes/commerce_customer.checkout_pane.inc @@ -22,6 +22,8 @@ function commerce_customer_profile_pane_settings_form($checkout_pane) { foreach (commerce_info_fields('commerce_customer_profile_reference', 'commerce_order') as $name => $field) { if ($type == $field['settings']['profile_type']) { $instance = field_info_instance('commerce_order', $name, 'commerce_order'); + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); $options[$name] = check_plain($instance['label']); } } diff --git a/modules/line_item/commerce_line_item.module b/modules/line_item/commerce_line_item.module index a0525de..2c256eb 100644 --- a/modules/line_item/commerce_line_item.module +++ b/modules/line_item/commerce_line_item.module @@ -713,6 +713,9 @@ function commerce_line_item_field_info() { * valid line item ID). */ function commerce_line_item_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); + // Extract line_item_ids to check. $line_item_ids = array(); diff --git a/modules/order/commerce_order.rules.inc b/modules/order/commerce_order.rules.inc index 34c69d6..8a612fc 100644 --- a/modules/order/commerce_order.rules.inc +++ b/modules/order/commerce_order.rules.inc @@ -143,6 +143,8 @@ function commerce_order_address_field_options_list() { if (in_array($type, $address_field['bundles']['commerce_customer_profile'])) { // Add it to the options list. $instance = field_info_instance('commerce_customer_profile', 'commerce_customer_address', $type); + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); $options[commerce_customer_profile_type_get_name($type)][$field_name . '|' . $address_field_name] = check_plain($instance['label']); } diff --git a/modules/price/commerce_price.module b/modules/price/commerce_price.module index 6955b36..d5b1e34 100644 --- a/modules/price/commerce_price.module +++ b/modules/price/commerce_price.module @@ -62,6 +62,9 @@ function commerce_price_field_info() { * Implements hook_field_validate(). */ function commerce_price_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items, &$errors) { + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); + // Ensure only numeric values are entered in price fields. foreach ($items as $delta => &$item) { if (!empty($item['amount']) && !is_numeric($item['amount'])) { @@ -382,7 +385,10 @@ function commerce_price_field_formatter_prepare_view($entity_type, $entities, $f /** * Implements hook_field_formatter_view(). */ -function commerce_price_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { +function commerce_price_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display, $requested_language = NULL) { + // Integration with i18n fields (use the language the content is displayed in). + commerce_object_translate('field_instance', $instance, $requested_language); + $element = array(); // Loop through each price value in this field. diff --git a/modules/product_reference/commerce_product_reference.module b/modules/product_reference/commerce_product_reference.module index 71181cd..622ca10 100644 --- a/modules/product_reference/commerce_product_reference.module +++ b/modules/product_reference/commerce_product_reference.module @@ -282,6 +282,9 @@ function commerce_product_reference_field_instance_settings_form($field, $instan * product id, or the product is not referenceable). */ function commerce_product_reference_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { + // Integration with i18n fields (use the current interface language). + commerce_object_translate('field_instance', $instance); + // Extract product_ids to check. $product_ids = array();