Welcome Guest, Not a member yet? Register   Sign In
A better language helper
#1

[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;
}


Messages In This Thread
A better language helper - by El Forum - 12-16-2011, 02:28 PM



Theme © iAndrew 2016 - Forum software by © MyBB