1

Why is t() not working as expected in thre following situation?

I am using Geolocation field module for D7 (drupal.org/project/geolocation). The strings inside this module which are used in the widget don't appear in the translate interface searches. All my other modules' strings are correctly showing up in translate interface. Also the strings coming from JS for that module (that are enclosed in Drupal.t()) works ok.

For example "Get location" string is not translatable.

Here is a part of the module:

function geolocation_googlemaps_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // In order to make Geolocation field work with the popular Field collection module
  // we check if our map widget is part of a Field collection and add the 
  // #field_parents delta.
  if ($instance['entity_type'] == 'field_collection_item') {
    $depth = count($element['#field_parents']) - 1;
    $parent_delta = $element['#field_parents'][$depth];
    $id = $instance['id'] . '-' . $parent_delta . '-' . $delta;
  }
  else {
    $id = $instance['id'] . '-' . $delta;
  }

  $lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : NULL;
  // To make this widget compatible with geofiled we need to rename the keys for
  // longitude. Geofield uses "lon" while Geolocation Field uses "lng".
  if ($field['type'] == 'geofield') {
    $lng_value = isset($items[$delta]['lon']) ? $items[$delta]['lon'] : NULL;
  } else {
    $lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : NULL;
  }
  $element += array(
    '#delta' => $delta,
  );

  switch ($instance['widget']['type']) {

    case 'geolocation_googlemap':
      $element['address'] = array(
        '#type' => 'item',
        '#title' => $element['#title'],
        '#prefix' => '<div id="geolocation-address-' . $id . '" class="geolocation-address">',
        '#suffix' => '</div>',
        '#required' => $instance['required'],
      );
      $element['address']['field'] = array(
        '#type' => 'textfield',
        '#maxlength' => 120,
      );
      $element['address']['geocode'] = array(
        '#prefix' => '<span id="geolocation-address-geocode-' . $id . '" class="geolocation-address-geocode">',
        '#suffix' => '</span>',
        '#markup' => t('Get location'),
      );
      $element['help'] = array(
        '#prefix' => '<div id="geolocation-help-' . $id . '" class="geolocation-help">',
        '#suffix' => '</div>',
        '#markup' => t('Enter an address / location in the textfield or you can also click on the map to set a marker'),
      );
      $element['googlemap'] = array(
        '#prefix' => '<div id="geolocation-map-' . $id . '" class="geolocation-map" style="width:100%;height:400px;">',
        '#suffix' => '</div>',
      );
      // Presentational item.
      $element['latitem'] = array(
        '#type' => 'item',
        '#title' => t('Latitude:'),
        '#prefix' => '<div id="geolocation-lat-item-' . $id . '" class="geolocation-lat-item">',
        '#suffix' => '</div>',
        '#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
        '#required' => $instance['required'],
      );
      $element['lat'] = array(
        '#type' => 'hidden',
        '#prefix' => '<div id="geolocation-lat-' . $id . '" class="geolocation-lat">',
        '#suffix' => '</div>',
        '#default_value' => $lat_value,
      );
      // Presentational item.
      $element['lngitem'] = array(
        '#type' => 'item',
        '#title' => t('Longitude:'),
        '#prefix' => '<div id="geolocation-lng-item-' . $id . '" class="geolocation-lng-item">',
        '#suffix' => '</div>',
        '#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
        '#required' => $instance['required'],
      );
      $element['lng'] = array(
        '#type' => 'hidden',
        '#prefix' => '<div id="geolocation-lng-' . $id . '" class="geolocation-lng">',
        '#suffix' => '</div>',
        '#default_value' => $lng_value,
      );
      $element['remove'] = array(
        '#prefix' => '<div id="geolocation-remove-' . $id . '" class="geolocation-remove"><span>',
        '#suffix' => '</span></div>',
        '#markup' => t('Remove'),
      );

      // Attach CSS and JS files via FAPI '#attached'.
      $element['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
      $element['googlemap']['#attached']['js'][] = array(
        'data' => '//maps.google.com/maps/api/js?sensor=false',
        'type' => 'external',
      );
      $element['googlemap']['#attached']['js'][] = array(
        'data' => '//www.google.com/jsapi',
        'type' => 'external',
      );
      $element['googlemap']['#attached']['js'][] = array(
        'data' => drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps_widget.js',
        'type' => 'file',
        'scope' => 'footer',
      );

      // Make defaults available as javascript settings. In JS files use:
      // Drupal.settings.mapDefaults.lat
      $map_defaults_lat = ($lat_value) ? $lat_value : '';
      $map_defaults_lng = ($lng_value) ? $lng_value : '';
      $map_defaults = array(
        $id => array(
          'lat' => $map_defaults_lat,
          'lng' => $map_defaults_lng,
        ),
      );
      $data = array(
        'defaults' => $map_defaults,
        'settings' => $instance['widget']['settings'],
      );
      $element['googlemap']['#attached']['js'][] = array(
        'data' => array('geolocation' => $data),
        'type' => 'setting',
      );

      $element['field_type'] = array('#type' => 'value', '#value' => $field['type']);
      $element['#element_validate'] = array('geolocation_googlemaps_field_widget_validate');
      $element['#element_validate'][] = 'geolocation_googlemaps_field_widget_set_value';

      break;
  }
  return $element;
}
1
  • There isn't actually a question here. Perhaps you could edit your post to contain a question?
    – Chapabu
    Nov 8, 2012 at 10:58

1 Answer 1

3

It happens. Try to flush cache and open page with this form (googlemaps widget settings) in language other than English.
After the first request for translation strings should appear.

2
  • Thanks, it appears that the strings are only translatable upon visiting the relevant page in another language. I dont know how "stringts override" module (drupal.org/project/stringoverrides) can be used if you have only one language though. Nov 8, 2012 at 15:27
  • @SinanErdem, String Overrides is UI for core locale_custom_strings_LANG config. “To find the strings you can actually change, open up a module and look for t() function calls. ” — from help.
    – kalabro
    Nov 9, 2012 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.