|MediaWiki file: LocalisationCache.php|
|Source code:||master • 1.35.0 • 1.34.4 • 1.31.10|
The LocalisationCache class handles caching of PHP source code messages (as mentioned above, only the MessageCache layer handles database-overridden messages). This class is not called directly; rather the MessageCache class calls Language::getMessage(), which then calls this class to get the message. This layer handles the following logic:
- Lazily loading messages from the back-end cache
- Re-caching messages
- Resolving the language fallback sequence
Before MediaWiki 1.23, a PHP associative array was used for storing the messages. A global configuration in $wgExtensionMessagesFiles contains the mapping extension name to the filename where messages can be found.
Since MediaWiki 1.23, use of this variable to define messages is discouraged; instead, store messages in JSON format and use $wgMessagesDirs. For setting other variables than $messages, $wgExtensionMessagesFiles should still be used. If there is an entry in $wgMessagesDirs with the same key as one in $wgExtensionMessagesFiles, then any $messages variables set in the $wgExtensionMessagesFiles file will be ignored. This means an extension that only provides messages can be backwards compatible by using both $wgExtensionMessagesFiles and $wgMessagesDirs, and only one of the two will be used depending on what the version of MediaWiki supports.
$wgMessagesDirs is an associative array mapping extension name to the path of the directory where message files can be found. The message files are expected to be JSON files named for their language code, e.g. en.json, de.json, etc. Extensions with messages in multiple places may specify an array of message directories.
LocalisationCache reads the localization messages from the files as per the above configuration for recaching.
Resolving language fallbacks
How this class resolves language fallbacks is that when searching for a specific message key, it will look for the message in all the languages, and then cache the value as if it were from the original language. The reason behind this is that each language has its own back-end cache. For a fallback sequence, if a message doesn't exist, it would normally mean having to load and query each cache individually every time the message is accessed. To avoid this, the fallback chain is resolved once when the messages are re-cached from the original PHP files, and the final value is stored in the cache for the top-level language.