Jump to content

User:APatro (WMF)/Message Bundle Lua Integration/Testing

From mediawiki.org

This document describes the setup process for testing the Translate extension's Lua module to interact with message bundles.

Quick setup

[edit]
<?php
$wgTranslateEnableMessageBundleIntegration = true;
$wgPageLanguageUseDB = true;
<?php
$wgTranslateEnableLuaIntegration = true;

Create a message bundle

[edit]

Next, let's create a message bundle:

  • Navigate to: MessageBundleDemoForever
  • Paste the following content. It creates a message bundle with label Message bundle demo forever.
{
  "@metadata": {
    "label": "Message bundle demo forever"
  },
  "template-description": "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.",
  "label": "Add the question text, or prompt, here. This text is required.",
  "description": "You can add an optional tip or note related to the prompt like this.",
  "choice-1": "a correct answer",
  "choice-2": "an incorrect answer",
  "choice-3": "an incorrect answer! - FR",
  "choice-4": "a correct answer",
  "choice-5": "Another incorrect answer ...",
  "display-name": "Checkboxes"
}

Using the integration

[edit]
  • Create a module page Module:TestLuaIntegration
local p = {};

-- Load the Translate lua library
local TMB = require 'mw.ext.translate.messageBundle'

function p.testDefaultLanguage ()
  local mb = mw.ext.translate.messageBundle.new( "MessageBundleDemoForever" )
  return mb:t( "template-description" )
end

function p.testLanguage ()
  local mb = TMB.new( "MessageBundleDemoForever", "es" )
  -- Should fetch Spanish translations, if available, else English
  return mb:t( "description" )
end

function p.testWithoutFallback ()
  local mb = TMB.newWithoutFallbacks( "MessageBundleDemoForever", "fr" )
  -- Since fallbacks are disabled, loads French translation if available else nil
  return mb:t( "description" )
end

-- Simple test function
function p.hello () 
  return 'hello'
end

return p
  • Use the newly created module in any test page with the following content:
----
TestLuaIntegration Module: <code>hello</code> function:

{{#invoke:TestLuaIntegration|hello}}
----
TestLuaIntegration Module: <code>testDefaultLanguage</code> function:

{{#invoke:TestLuaIntegration|testDefaultLanguage}}
----
TestLuaIntegration Module: <code>testLanguage</code> function:

{{#invoke:TestLuaIntegration|testLanguage}}
----
TestLuaIntegration Module: <code>testWithoutFallback</code> function:

{{#invoke:TestLuaIntegration|testWithoutFallback}}
----

You can update Module:TestLuaIntegration to accept the message bundle name, and the key in the message bundle to fetch translation for.

Some additional reading

[edit]