diff --git a/commerce_flat_rate.i18n.inc b/commerce_flat_rate.i18n.inc new file mode 100644 index 0000000..1cc407a --- /dev/null +++ b/commerce_flat_rate.i18n.inc @@ -0,0 +1,52 @@ + t('Commerce Flat Rate'), + 'key' => 'name', + 'class' => 'i18n_string_object_wrapper', + 'string translation' => array( + 'textgroup' => 'commerce_flat_rate', + 'type' => 'name', + 'properties' => array( + 'title' => t('Title'), + 'display_title' => t('Display title', array(), array('context' => 'title for display purposes')), + 'description' => t('Description'), + ), + ), + ); + return $info; +} + +/** + * Implements hook_i18n_string_info(). + */ +function commerce_flat_rate_i18n_string_info() { + $groups['commerce_flat_rate'] = array( + 'title' => t('Commerce Flat Rate'), + 'description' => t('Commerce Flat Rate shipping service titles, display titles, and descriptions.'), + 'format' => FALSE, // This group doesn't have strings with format + 'list' => FALSE, // This group cannot list all strings + ); + return $groups; +} + +/** + * Implements hook_i18n_string_objects(). + */ +function commerce_flat_rate_i18n_string_objects($type) { + if ($type == 'commerce_flat_rate') { + return db_select('commerce_flat_rate_service', 'cfrs') + ->fields('cfrs', array('name', 'title', 'display_title', 'description')) + ->execute() + ->fetchAllAssoc('name', PDO::FETCH_ASSOC); + } +} diff --git a/commerce_flat_rate.module b/commerce_flat_rate.module index 46959a0..ad0b8cb 100644 --- a/commerce_flat_rate.module +++ b/commerce_flat_rate.module @@ -114,6 +114,7 @@ function commerce_flat_rate_commerce_shipping_method_info() { * Implements hook_commerce_shipping_service_info(). */ function commerce_flat_rate_commerce_shipping_service_info() { + global $language; $services = array(); // Look for flat rate services currently defined in the database. @@ -155,6 +156,15 @@ function commerce_flat_rate_commerce_shipping_service_info() { $base_rate['data'] = commerce_price_component_add($base_rate, $tax_rate['price_component'], $component, TRUE); } + $untranslated = array( + 'title' => $service['title'], + 'display_title' => !empty($service['display_title']) ? $service['display_title'] : $service['title'], + 'description' => $service['description'], + ); + + if (module_exists('i18n_string')) { + $service = i18n_object('commerce_flat_rate', $service)->translate($language->language); + } // Add the full service array to our return value. $services[$name] = array( 'title' => $service['title'], @@ -168,6 +178,7 @@ function commerce_flat_rate_commerce_shipping_service_info() { ), 'base_rate' => $base_rate, 'data' => $data, + 'untranslated_strings' => $untranslated, ); } } diff --git a/includes/commerce_flat_rate.admin.inc b/includes/commerce_flat_rate.admin.inc index 40f18f2..2157caf 100644 --- a/includes/commerce_flat_rate.admin.inc +++ b/includes/commerce_flat_rate.admin.inc @@ -15,6 +15,16 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) // Store the initial shipping service in the form state. $form_state['shipping_service'] = $shipping_service; + $defaults = array( + 'untranslated_strings' => array( + 'title' => '', + 'display_title' => '', + 'description' => '' + ) + ); + + $shipping_service += $defaults; + $form['flat_rate'] = array( '#tree' => TRUE, ); @@ -22,7 +32,7 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) $form['flat_rate']['title'] = array( '#type' => 'textfield', '#title' => t('Title'), - '#default_value' => $shipping_service['title'], + '#default_value' => $shipping_service['untranslated_strings']['title'], '#description' => t('The administrative title of this flat rate. It is recommended that this title begin with a capital letter and contain only letters, numbers, and spaces.'), '#required' => TRUE, '#size' => 32, @@ -53,8 +63,8 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) $form['flat_rate']['display_title'] = array( '#type' => 'textfield', - '#title' => t('Display title'), - '#default_value' => $shipping_service['display_title'], + '#title' => t('Display title', array(), array('context' => 'title for display purposes')), + '#default_value' => $shipping_service['untranslated_strings']['display_title'], '#description' => t('The front end display title of this flat rate shown to customers. Leave blank to default to the Title from above.'), '#size' => 32, ); @@ -63,7 +73,7 @@ function commerce_flat_rate_service_form($form, &$form_state, $shipping_service) '#type' => 'textarea', '#title' => t('Description'), '#description' => t('Describe this flat rate if necessary. The text will be displayed in the flat rate services overview table.'), - '#default_value' => $shipping_service['description'], + '#default_value' => $shipping_service['untranslated_strings']['description'], '#rows' => 3, ); @@ -225,6 +235,11 @@ function commerce_flat_rate_service_form_submit($form, &$form_state) { else { drupal_set_message(t('Flat rate service saved.')); $form_state['redirect'] = 'admin/commerce/config/shipping/services/flat-rate'; + + // Update string translations. + if (module_exists('i18n_string')) { + i18n_string_object_update('commerce_flat_rate', $shipping_service); + } } }