Elgg\I18n\Translator::addTranslation PHP Method

addTranslation() public method

Translations are arrays in the Zend Translation array format, eg: $english = array('message1' => 'message1', 'message2' => 'message2'); $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
public addTranslation ( string $country_code, array $language_array ) : boolean
$country_code string Standard country code (eg 'en', 'nl', 'es')
$language_array array Formatted array of strings
return boolean Depending on success
    public function addTranslation($country_code, $language_array)
    {
        if (!isset($GLOBALS['_ELGG']->translations)) {
            $GLOBALS['_ELGG']->translations = array();
        }
        $country_code = strtolower($country_code);
        $country_code = trim($country_code);
        if (is_array($language_array) && $country_code != "") {
            if (sizeof($language_array) > 0) {
                if (!isset($GLOBALS['_ELGG']->translations[$country_code])) {
                    $GLOBALS['_ELGG']->translations[$country_code] = $language_array;
                } else {
                    $GLOBALS['_ELGG']->translations[$country_code] = $language_array + $GLOBALS['_ELGG']->translations[$country_code];
                }
            }
            return true;
        }
        return false;
    }

Usage Example

Beispiel #1
0
 public function testIssuesNoticeOnMissingKey()
 {
     // key is missing from all checked translations
     _elgg_services()->logger->disable();
     $this->assertEquals("{$this->key}b", $this->translator->translate("{$this->key}b"));
     $logged = _elgg_services()->logger->enable();
     $this->assertEquals([['message' => "Missing English translation for \"{$this->key}b\" language key", 'level' => Logger::NOTICE]], $logged);
     // has fallback key
     $this->translator->addTranslation('en', ["{$this->key}b" => 'Dummy']);
     _elgg_services()->logger->disable();
     $this->assertEquals('Dummy', $this->translator->translate("{$this->key}b", [], 'es'));
     $logged = _elgg_services()->logger->enable();
     $this->assertEquals([['message' => "Missing es translation for \"{$this->key}b\" language key", 'level' => Logger::NOTICE]], $logged);
 }
All Usage Examples Of Elgg\I18n\Translator::addTranslation