From 98594631cc6216dbfd93afd2f8cfecb421cf15d1 Mon Sep 17 00:00:00 2001
From: Christian Spitzlay <christian.spitzlay@bio.logis.de>
Date: Wed, 17 Oct 2012 11:22:48 +0200
Subject: [PATCH] [#1270624] cspitzlay: make user-defined strings in commerce
 tax translatable via i18n_string (tax rates and tax types
 independently)

---
 modules/tax/commerce_tax.i18n.inc | 69 +++++++++++++++++++++++++++++++++++++++
 modules/tax/commerce_tax.module   | 11 ++++++-
 2 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 modules/tax/commerce_tax.i18n.inc

diff --git a/modules/tax/commerce_tax.i18n.inc b/modules/tax/commerce_tax.i18n.inc
new file mode 100644
index 0000000..0e804ed
--- /dev/null
+++ b/modules/tax/commerce_tax.i18n.inc
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * Implements hook_i18n_object_info().
+ */
+function commerce_tax_i18n_object_info() {
+  $info['commerce_tax_rate'] = array(
+    'title' => t('Commerce tax rate'),
+    'key' => 'rules_component',
+    'class' => 'i18n_string_object_wrapper',
+    'string translation' => array(
+      'textgroup' => 'commerce_tax',
+      'type' => 'name',
+      'properties' => array(
+        'title' => t('Title'),
+        'display_title' => t('Display title', array(), array('context' => 'title for display purposes')),
+        'description' => t('Description'),
+      ),
+    ),
+  );
+  $info['commerce_tax_type'] = array(
+    'title' => t('Commerce tax type'),
+    'key' => 'rule',
+    'class' => 'i18n_string_object_wrapper',
+    'string translation' => array(
+      'textgroup' => 'commerce_tax',
+      '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_tax_i18n_string_info() {
+  $groups['commerce_tax'] = array(
+    'title' => t('Commerce tax'),
+    'description' => t("Translatable user-defined strings of the commerce tax module."),
+    '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_tax_i18n_string_objects($type) {
+  if ($type == 'commerce_tax_rate') {
+    $query = db_select('commerce_tax_rate', 'ctr')
+      ->fields('ctr', array('name', 'title', 'display_title', 'description'));
+    $query->addExpression("CONCAT('commerce_tax_rate_', ctr.name)", 'rules_component');
+    return $query->execute()
+      ->fetchAllAssoc('rules_component', PDO::FETCH_ASSOC);
+  }
+  if ($type == 'commerce_tax_type') {
+    $query = db_select('commerce_tax_type', 'ctt')
+      ->fields('ctt', array('name', 'title', 'display_title', 'description'));
+    $query->addExpression("CONCAT('commerce_tax_type_', ctt.name)", 'rule');
+    return $query->execute()
+      ->fetchAllAssoc('rule', PDO::FETCH_ASSOC);
+  }
+}
\ No newline at end of file
diff --git a/modules/tax/commerce_tax.module b/modules/tax/commerce_tax.module
index bb56e77..3b222be 100644
--- a/modules/tax/commerce_tax.module
+++ b/modules/tax/commerce_tax.module
@@ -76,6 +76,11 @@ function commerce_tax_types() {
         );
 
         $tax_types[$name] = array_merge($defaults, $tax_type);
+        if (module_exists('i18n_string')) {
+          // translate user-defined strings
+          global $language;
+          $tax_types[$name] = i18n_object('commerce_tax_type', $tax_types[$name])->translate($language->language);
+        }
       }
     }
 
@@ -168,8 +173,12 @@ function commerce_tax_rates() {
           'calculation_callback' => 'commerce_tax_rate_calculate',
           'module' => $module,
         );
-
         $tax_rates[$name] = array_merge($defaults, $tax_rate);
+        if (module_exists('i18n_string')) {
+          // translate user-defined strings
+          global $language;
+          $tax_rates[$name] = i18n_object('commerce_tax_rate', $tax_rates[$name])->translate($language->language);
+        }
       }
     }
 
-- 
1.7.12.2

