I have an application with a support of 3 languages. If I switch the language and if the text doesn't have a translation in the current language it will be not shown. But I want to show the translation of the default language instead of not showing anything. How to do that?

Thank you.

有帮助吗?

解决方案

I would extend the core class CI_Lang with my own.

class MY_Lang extends CI_Lang {

    var $defaultLanguage = array();

    function __construct()
    {
        parent::__construct();                      
    }

    function line($line = '')
    {
        // Get the value from the current language.
        $value = parent::line($line);

        // Fallback on default language if not found.
        if ($value === FALSE) {                             
            $value = $this->defaultLanguage[$line];
        }

        return $value;
    }

    function loadDefault($langfile = '', $idiom = '') {         
        $this->defaultLanguage = array_merge($this->defaultLanguage, (array)$this->load($langfile, $idiom, TRUE));
    }    
}

Then load the default language files I want in my controller.

$this->lang->load('error', 'french');          // Standard way
$this->lang->loadDefault('error', 'english');  // New way for defaults

That way, if you query for something that is not in the french translation, it will default to the english one.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top