Skip to content

Instantly share code, notes, and snippets.

@joubertredrat
Last active April 18, 2016 18:22
Show Gist options
  • Save joubertredrat/211dbd16d0194af5af17 to your computer and use it in GitHub Desktop.
Save joubertredrat/211dbd16d0194af5af17 to your computer and use it in GitHub Desktop.
<?php
class App {
/**
* Translates string.
* Examples:
* echo \App::translate('Páginas');
* > Pages
* echo \App::translate('Oi %s, é Domingo! Seu e-mail %s está cheio', 'Dev', 'lol@dev.us');
* > Hi Dev, is Sunday! Your mail lol@dev.us is full
* echo \App::translate('Boa tarde');
* > Boa tarde <= hasn't been translated because they do not have a translation for this word
* echo \App::translate('Ontem');
* > Yesterday
*
* @param string $text text to translate.
* @param string replaces
* @return string
* @todo This is experimental
*/
public static function translate($text)
{
$replace = func_get_args();
unset($replace[0]);
$ci =& get_instance();
$session = $ci->auth->get_session();
$lang = $session['user_idioma'];
if ($lang != LANGUAGE_DEFAULT) {
$ci->lang->load('app', LANGUAGE_DEFAULT);
$key = array_search($text, $ci->lang->language);
$ci->lang->load('app', $lang);
if ($ci->lang->line($key))
$text = $ci->lang->line($key);
}
if ($replace)
$text = vsprintf($text, $replace);
return $text;
}
}
<?php
$lang['pages'] = 'Páginas';
$lang['saudation'] = 'Oi %s, é Domingo! Seu e-mail %s está cheio';
$lang['random_index'] = 'Ontem';
<?php
$lang['pages'] = 'Pages';
$lang['saudation'] = 'Hi %s, is Sunday! Your mail %s is full';
$lang['random_index'] = 'Yesterday';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment