CodeIgniter Forums

Full Version: A better language helper
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I am working on integrating with getlocalization.com to crowd-source localization for wookmark.com, and found the CodeIgniter language helper function not very useful. Here's a version that works better for my purposes, that I wanted to share.

One difference is that the function returns the passed in text when no localization is available. CodeIgniter's lang function returns false in that case. I prefer this option since it's a better failsave.

Second difference is that the function supports templated parameters. Here's an example.
Code:
trans('Hello {user}', array('user' => 'Peter')); //  Returns "Hello Peter"

And here's the helper function.
Code:
function trans($line, $props = null) {
  $result = $line;
  $CI =& get_instance();
  if(isset($CI->lang->language[$line])) {
    $result = $CI->lang->language[$line];
  }
  if($props) {
    foreach($props as $key=>$value) {
      $bits = explode('{'.$key.'}', $result);
      $result = implode($value, $bits);
    }
  }
  return $result;
}