MediaWiki master
MediaWiki\Rest\JsonLocalizer Class Reference

Utility class for json localization needs in the REST API. More...

Public Member Functions

 __construct (ResponseFactory $responseFactory)
 
 getFormattedMessage ( $message)
 Tries to return one formatted string for a message key or message value object.
 
 localizeJson (array $json)
 Recursively localizes name/value pairs, where the name begins with "x-i18-n-".
 
 localizeValue (array $json, string $key)
 Returns the localized value if possible, or the non-localized value if one is available, or null otherwise.
 

Detailed Description

Utility class for json localization needs in the REST API.

Much of this involves replacing custom name/value pairs (herein referred to as i18n pairs) with translated standard name/value pairs (herein referred to as schema pairs). For example, OpenAPI specifications include a "description" field represented as a name/value pair, like: "description": "My description". We allow a "x-i18n-description" field, whose value is a MediaWiki message key, like: "x-i18n-description": "rest-my-description-message-key" Functions in this class will replace the "x-i18n-description" with a translated "description".

Definition at line 18 of file JsonLocalizer.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Rest\JsonLocalizer::__construct ( ResponseFactory $responseFactory)
Parameters
ResponseFactory$responseFactory
Access: internal

Definition at line 28 of file JsonLocalizer.php.

Member Function Documentation

◆ getFormattedMessage()

MediaWiki\Rest\JsonLocalizer::getFormattedMessage ( $message)

Tries to return one formatted string for a message key or message value object.

Parameters
string | MessageValue$messagethe message format, or a key representing it
Returns
string

Definition at line 111 of file JsonLocalizer.php.

◆ localizeJson()

MediaWiki\Rest\JsonLocalizer::localizeJson ( array $json)

Recursively localizes name/value pairs, where the name begins with "x-i18-n-".

Parameters
array$jsonthe input json, as a PHP array
Returns
array the adjusted json, or the unchanged json if no adjustments were made

Definition at line 41 of file JsonLocalizer.php.

◆ localizeValue()

MediaWiki\Rest\JsonLocalizer::localizeValue ( array $json,
string $key )

Returns the localized value if possible, or the non-localized value if one is available, or null otherwise.

Translates only top-level keys. Useful when the value corresponding to the input key may be either a string to be used as-is or a MessageValue object to be localized.

Parameters
array$jsonthe input json, as a PHP array
string$keykey name of the field
Returns
?string

Definition at line 90 of file JsonLocalizer.php.


The documentation for this class was generated from the following file: