From 875661b14bad121b2f01e53cb680e4b74b180f4d Mon Sep 17 00:00:00 2001
From: M Parker <mparker17@536298.no-reply.drupal.org>
Date: Tue, 20 May 2014 15:47:45 -0400
Subject: [PATCH] 2234915-4

---
 includes/registration.field.inc | 36 +++++++++++++++++++++++-
 registration.module             | 62 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 97 insertions(+), 1 deletion(-)

diff --git a/includes/registration.field.inc b/includes/registration.field.inc
index 9a451bc..e0f9bab 100644
--- a/includes/registration.field.inc
+++ b/includes/registration.field.inc
@@ -152,6 +152,7 @@ function registration_field_formatter_info() {
       'field types' => array('registration'),
       'settings' => array(
         'label' => ' ',
+        'i18n_string_key' => '',
       ),
     ),
     'registration_link' => array(
@@ -159,6 +160,7 @@ function registration_field_formatter_info() {
       'field types' => array('registration'),
       'settings' => array(
         'label' => ' ',
+        'i18n_string_key' => '',
       ),
     ),
     'registration_form' => array(
@@ -186,6 +188,38 @@ function registration_field_formatter_settings_form($field, $instance, $view_mod
       '#required' => FALSE,
       '#description' => t("Optional label to use when displaying the registration title or link. Leave blank to use the parent event's label."),
     );
+
+    // Store a key so we can store/retrieve translated strings for this field
+    // formatter instance.
+    $element['i18n_string_key'] = array('#type' => 'value', '#value' => implode(':', array(
+      $instance['entity_type'],
+      $instance['bundle'],
+      $view_mode,
+      $instance['field_name'],
+    )));
+  }
+
+  // Since we have translatable strings, we'll need to register them when the
+  // form is submitted.
+  $element['#process'][] = 'registration_field_formatter_settings_form_process';
+
+  return $element;
+}
+
+/**
+ * Form element process handler for registration_field_formatter_settings_form().
+ */
+function registration_field_formatter_settings_form_process($element, &$form_state, &$form) {
+  // For reasons I don't fully understand, when you click the gear button to
+  // open the settings, $form_state['submitted'] === TRUE; but after you set the
+  // settings and click the 'Update' button, $form_state['submitted'] === FALSE.
+  // Furthermore, it's impossible to add a submit handler to this sub-form or
+  // the 'Manage Display' form as a whole.
+  //
+  // Anyway, to avoid blowing away the string translation if the user just
+  // wants to look at the string without changing it.
+  if ($form_state['submitted'] === FALSE) {
+    _registration_translate_update($element['i18n_string_key']['#value'] . ':label', $element['label']['#default_value']);
   }
 
   return $element;
@@ -222,7 +256,7 @@ function registration_field_formatter_view($entity_type, $entity, $field, $insta
   if (isset($items[0]['registration_type']) && !empty($items[0]['registration_type'])) {
     $reg_type = registration_type_load($items[0]['registration_type']);
     $settings = $display['settings'];
-    $label = !empty($settings['label']) ? $settings['label'] : $reg_type->label;
+    $label = !empty($settings['label']) ? _registration_translate($settings['i18n_string_key'] . ':label', $settings['label']) : $reg_type->label;
 
     switch ($display['type']) {
       case 'registration_default':
diff --git a/registration.module b/registration.module
index 65f1541..78fad0c 100644
--- a/registration.module
+++ b/registration.module
@@ -1804,3 +1804,65 @@ function registration_type_delete(RegistrationType $type) {
 function registration_state_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
   return user_access('administer registration states', $account);
 }
+
+/**
+ * Implements hook_i18n_string_info().
+ */
+function registration_i18n_string_info() {
+  $groups = array();
+
+  $groups['registration'] = array(
+    'title' => t('Entity registration'),
+    'description' => t('Translatable registration settings.'),
+    'format' => FALSE,
+    'list' => TRUE,
+  );
+
+  return $groups;
+}
+
+/**
+ * Translates a config string to the current language or to a given language.
+ *
+ * @param $name string
+ *   A key *without* a textgroup (this module's textgroup will be prefixed
+ *   automatically).
+ * @param $string string
+ *   A user-entered configuration string in the default language. The default
+ *   language may or may not be English.
+ * @param $langcode string
+ *   The language code. Defaults to NULL.
+ *
+ * @return string
+ *   The translated string, if i18n_string() is available. Otherwise, returns
+ *   $string.
+ *
+ * @see https://drupal.org/node/1114010
+ * @see i18n_string()
+ */
+function _registration_translate($name, $string, $langcode = NULL) {
+  return function_exists('i18n_string') ? i18n_string('registration:' . $name, $string, array('langcode' => $langcode)) : $string;
+}
+
+/**
+ * Registers a config string so it can later be translated.
+ *
+ * @param $name string
+ *   A key *without* a textgroup (this module's textgroup will be prefixed
+ *   automatically).
+ * @param $string string
+ *   A user-entered configuration string in the default language. The default
+ *   language may or may not be English.
+ * @param $langcode string
+ *   The language code. Defaults to NULL.
+ *
+ * @return string
+ *   The translated string, if i18n_string() is available. Otherwise, returns
+ *   $string.
+ *
+ * @see https://drupal.org/node/1114010
+ * @see i18n_string()
+ */
+function _registration_translate_update($name, $string, $langcode = NULL) {
+  return function_exists('i18n_string') ? i18n_string('registration:' . $name, $string, array('update' => TRUE, 'langcode' => $langcode)) : FALSE;
+}
-- 
1.9.2

