Jump to content

User:APatro (WMF)/Message Bundle Lua Integration

From mediawiki.org

Background information

[edit]

Why translatable modules?

[edit]

Translatable modules is a project to define, implement, document, and deploy a framework to improve the ability to translate and share modules. The goal is for user interface strings (messages) in Scribunto modules on multilingual wiki sites, such as Wikidata and Commons, to be easily localized, similarly to core MediaWiki, extensions, and translatable pages.

Read more at Translatable modules

What are message bundles?

[edit]

Message bundles contain strings in key-value JSON format that, representing a message group that can be translated. Each key represents a translation unit and the value is the translation unit source that can be translated.

Read more at Help:Extension:Translate/Message Bundles

What is this project trying to do?

[edit]

We built message bundles to reduce the complexity around localization of Scribunto modules. Since Message bundles are JSON objects, they can be used to store translations that can then be queried and used in Scribunto modules.

This document describes a Lua module that is built into the Translate extension in order to provide Scribunto modules an API that they can use to query message bundles.

API structure

[edit]

You can review the patches submitted to add this feature on Gerrit, and the corresponding Phabricator task.

Given the following message bundle, in a page named MessageBundleDemoForever ,

{
  "@metadata": { 
    "sourceLanguage": "fr",
	"priorityLanguages": ["es", "it"]
  },
  "problem.choiceresponse.p": "You can use this template as a guide to the simple editor markdown and OLX markup to use for checkboxes problems. Edit this component to replace this template with your own assessment.",
  "problem.choiceresponse.label": "Add the question text, or prompt, here. This text is required.",
  "problem.choiceresponse.description": "You can add an optional tip or note related to the prompt like this.",
  "problem.choiceresponse.checkboxgroup.choice.1": "a correct answer",
  "problem.choiceresponse.checkboxgroup.choice.2": "an incorrect answer",
  "problem.choiceresponse.checkboxgroup.choice.3": "an incorrect answer - FR",
  "problem.choiceresponse.checkboxgroup.choice.4": "a correct answer",
  "problem.choiceresponse.checkboxgroup.choice.5": "Another incorrect answer ...",
  "display_name": "Checkboxes"
}

To access the translation for the key: problem.choiceresponse.checkboxgroup.choice.5 in the page language in which the module is loaded, the following code can be used:

-- Load the translations in the page language in which the module is loaded
local mb = mw.ext.translate.messageBundle.new( "MessageBundleDemoForever" )

-- Access the needed key
mb:t( "problem.choiceresponse.checkboxgroup.choice.5" )

To load translations in a specific language, Spanish in this case:

-- Load the translations for the message bundle in Spanish
local mb = mw.ext.translate.messageBundle.new( "MessageBundleDemoForever", "es" )

-- Access the needed key
mb:t( "problem.choiceresponse.checkboxgroup.choice.5" )

In case a translation is missing in the page or requested language , language fallback will be used automatically, and if the translations are not available in the fallback language then the source language of the message bundle will be used.

To disable loading language fallback:

-- Load the translations in the page language, but skip using fallbacks
local mb = mw.ext.translate.messageBundle.new( "MessageBundleDemoForever", nil, true )

-- Access the needed key, without loading any fallback translations
mb:t( "problem.choiceresponse.checkboxgroup.choice.5" )

Future changes

[edit]

1. The methods in the module do not provide additional information about the translations, such as the current state (fuzzy, normal, reviewed etc) or which fallback language is being used in case the translation is not available in the requested language. We will be adding additional methods to provide these feature.