If there's no foo-bar flag, foo flag should be shown.

(e.g. en-gb => en)

CommentFileSizeAuthor
#2 languageicons-2251203-1.patch1.59 KBgeek-merlin

Comments

geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Status: Active » Needs review
StatusFileSize
new1.59 KB

This patch implements a fallback
* foo-bar => foo
* if there's no foo flag, it shows "unknown" flag

Used that on a live site.

Freso’s picture

Category: Bug report » Feature request

I like the premise of this patch, but it would be good to get some more testing of it. (And perhaps a unit test or two as well.)

mustanggb’s picture

What about having a separate function to get the image path, this way other modules (lang_dropdown) can benefit from being told the correct image to use.

mustanggb’s picture

Sorry for not rolling this into a patch, I'm not able to at the moment, but I was thinking something like this:

/**
 * Helper function to get the image path.
 */
function languageicons_get_image_path($language_code) {
  if ($path = variable_get('languageicons_path', drupal_get_path('module', 'languageicons') . '/flags/*.png')) {
    $sanitized_path = check_plain($path);
    $image_path = str_replace('*', $language_code, $sanitized_path);

     // If no icon fallback e.g. en-us => en.
    if (!file_exists($image_path)) {
      list($language_code) = explode('-', $language_code);
      $image_path = str_replace('*', $language_code, $sanitized_path);
       // If still no icon, try last fallback
      if (!file_exists($image_path)) {
        $image_path = str_replace('*', 'unknown', $sanitized_path);
        if (!file_exists($image_path)) {
          return;
        }
      }
    }

    return $image_path;
  }
}

/**
 * Theme language icon.
 *
 * This function can be overridden for no language icons.
 *
 * @seealso theme_image()
 */
function theme_languageicons_icon($variables) {
  $language = $variables['language'];
  $title    = $variables['title'];

  if (!$image_path = languageicons_get_image_path($language->language)) {
    $title = $title ? $title : $language->native;
    // Build up $image for theme_image() consumption.
    $image = array(
      'path' => $image_path,
      'alt' => $title,
      'title' => $title,
      'attributes' => array(
        'class' => array('language-icon'),
      ),
    );
    if ($size = check_plain(variable_get('languageicons_size', '16x12'))) {
      list($width, $height) = explode('x', $size);
      $image += array('width' => $width, 'height' => $height);
    }
    return theme('image', $image);
  }
}