yii\i18n\GettextMessageSource::loadMessages PHP Method

loadMessages() protected method

If translation for specific locale code such as en-US isn't found it tries more generic en. When both are present, the en-US messages will be merged over en. See [[loadFallbackMessages]] for details. If the $language is less specific than [[sourceLanguage]], the method will try to load the messages for [[sourceLanguage]]. For example: [[sourceLanguage]] is en-GB, $language is en. The method will load the messages for en and merge them over en-GB.
See also: loadFallbackMessages
See also: sourceLanguage
protected loadMessages ( string $category, string $language ) : array
$category string the message category
$language string the target language
return array the loaded messages. The keys are original messages, and the values are translated messages.
    protected function loadMessages($category, $language)
    {
        $messageFile = $this->getMessageFilePath($language);
        $messages = $this->loadMessagesFromFile($messageFile, $category);
        $fallbackLanguage = substr($language, 0, 2);
        $fallbackSourceLanguage = substr($this->sourceLanguage, 0, 2);
        if ($fallbackLanguage !== $language) {
            $messages = $this->loadFallbackMessages($category, $fallbackLanguage, $messages, $messageFile);
        } elseif ($language === $fallbackSourceLanguage) {
            $messages = $this->loadFallbackMessages($category, $this->sourceLanguage, $messages, $messageFile);
        } else {
            if ($messages === null) {
                Yii::error("The message file for category '{$category}' does not exist: {$messageFile}", __METHOD__);
            }
        }
        return (array) $messages;
    }

Usage Example

 /**
  * @inheritdoc
  */
 protected function loadMessages($category, $language)
 {
     if (!isset($this->scopeCache[$category])) {
         if (!is_null($this->scope)) {
             if (strripos($category, $this->scope) === FALSE) {
                 throw new \Exception("Invalid translation category {$category}, not in scope: " . $this->scope);
             }
             $this->scopeCache[$category] = substr($category, strlen($this->scope));
         } else {
             $this->scopeCache[$category] = $category;
         }
     }
     return parent::loadMessages($this->scopeCache[$category], $language);
 }