User:Sophivorus/sandbox

A cross-wiki module is a Lua module designed to be usable in every wiki without having to modify the source code. Cross-wiki modules are easy to adopt by new wikis and can be kept updated across Wikimedia wikis using the Synchronizer tool.

To become cross-wiki, modules must provide ways for wikis to localize everything they need to localize, without having to touch the code of the module itself. This page describes several techniques to do so.

Global vs cross-wiki
A global module would be a module that lives in a single wiki, but can be used in every wiki, similar to how files live in a single wiki (Commons) but can be used in every wiki. Global modules are not possible with current technology. However, using Synchronizer, cross-wiki modules can be almost as convenient. Furthermore, if global modules ever become possible, then having developed cross-wiki modules would make the transition simple, even trivial.

Best practices
This section describes some of the current best practices to develop cross-wiki modules.

Naming
This section can be ignored for modules designed to be called from templates only.

Cross-wiki modules that are meant to be used by other modules should be named the same in all wikis. This is to avoid dependency breaks between cross-wiki modules. For example, if a module named A requires a module named B, but in some wiki, module B is named C, then module A will not work in that wiki, unless the source code of module A is changed locally to require C instead of B.

If a local community doesn't want the cross-wiki name, or renaming is too much trouble, then one workaround is to create a "redirect module" with the cross-wiki name, that simply requires and returns the module with the local name.

(Luckily, the fact that the Module namespace is named differently in each language doesn't break dependencies, because "Module" is an alias for the Module namespace in all languages.)

Master module
Cross-wiki modules should pick one wiki where to do the development. Generally this will be the home wiki of the module, but it may migrate for various reasons, for example to increase the chances of recruiting new developers, by centralizing the development in a bigger or more appropriate wiki.

Initial comment
Cross-wiki modules should start with a comment that includes a link to the master module, and some kind of warning asking to contribute there rather than to the local version (example). This practice can not only prevent forking but may also help recruit new developers.

Sandbox
Cross-wiki modules should have a /sandbox subpage where to test out changes before deploying them on the main module and the other wikis.

Testcases
Cross-wiki modules should have a /testcases subpage with good unit tests to ensure high quality and stability of the module (example). Testcases should:


 * Use Module:ScribuntoUnit
 * Run with both the main module and the sandbox versions, so that we can compare the results (example)
 * Use require('strict') to avoid accidentally using non-declared variables
 * Output results both in /testcases/doc and the main /doc page of the module, to catch errors as early as possible

Documentation
Cross-wiki modules should have a /doc subpage with:


 * Documentation of all public functions of the module (example)
 * A section with the testcase runs for both the primary and the sandbox versions of the module (example)

Synchronization
Once a module is able to be copied unchanged to other wikis, the Synchronizer tool can be used to keep it synced across all Wikimedia wikis.

Using Template:Synchronize, each developer can build their own "dashboard" or "control panel" in their sandbox, in the master module documentation, or in a dedicated page or subpage anywhere they find convenient (example).

Backwards compatibility
Cross-wiki modules should keep development backwards-compatible. Changes that are not backwards-compatible will often require manual updates to each and every wiki, template and module that uses the module.

Localization of template parameters
Cross-wiki modules should have their parameters localized by the templates that call them. For example, consider the following module that simply outputs the given text (or "Example" if none is given): Then a Spanish template would localize the module like so: Notice that the template not only localizes the name of the "text" parameter ("texto" means "text" in Spanish), but also the default text ("Ejemplo" means "Example" in Spanish).

See Plantilla:Extracto for a real case of a template that localizes a cross-wiki module with this technique. Also, see Template:Excerpt for a case where a cross-wiki module is localized to the English Wikipedia, demonstrating that localization is not always the same as translation.

Localization of user-readable strings
Many modules need to output user-readable strings, such as error messages and interface elements (like buttons). Hard-coding the text of these strings forces other wikis to modify the code in order to localize them. To avoid this, developers should provide ways to localize user-readable strings without having to modify the code itself. This section explains several ways to achieve this.

Template parameters
User-readable strings can be localized through template parameters when calling the module. This approach is convenient when:


 * The text is likely to vary with each template call
 * The text is likely to be changed by users when calling the template
 * The text is likely to contain a magic word, a template call, a parser function or some other wiki element

An example of a module using this approach would be: This way, every template may modify the text when calling the module, like so: Notice that in this example, if a template calls the module without specifying the  parameter, then the hard-coded English text 'Example' would be used. This is not necessary. Modules may require template callers to set the  parameter by throwing an error if they don't. However, it's often friendlier to fallback to English.

Config file
Another way to localize user-readable strings is through a separate /config subpage. This approach is convenient when:


 * The module is meant to be called by many templates per wiki, thus allowing localization to be done only once and then reused
 * There're many messages to localize, so it's easier to have them all together in their own place
 * There's already a need for a /config file for other reasons, so we might as well use it for localization too

An example of a module using this approach would be: Then wikis would be able to create /config files like the following:

Translation tables
Another way to localize user-readable strings is through a central translation table at Commons. This approach is convenient when:


 * The strings should vary with the preferred language of the user, rather than the language of the wiki or page.
 * We want to centralize localization efforts on a single page.

The Module:TNT was created specifically to get strings from translation tables. An example module using TNT could look like this: See Data:I18n/Template:Graphs.tab for a simple but real example of a translation table with two messages, each having a single parameter. It's important to store parameters as parts of the strings because in many languages the parameter would have to be placed at a different position in the string according to the norms of the language.

Translation tables should start with the "Data:I18n/..." prefix to separate them from other types of tabular data. If a message has not yet been localized, TNT will fallback to English (or other fallback language as defined by the language's fallback sequence). TNT also supports all standard localization conventions such as NaN undefineds and other parameters.

One downside of this approach is that translation tables cannot be loaded from non-Wikimedia wikis, so relying only on this localization method prevents third-party wikis from using the module without modifications.

MediaWiki messages
In some cases, MediaWiki itself (or some extension) may have the messages we need already localized. For example, if we need the string "New page" we may use MediaWiki:Newpage, like so: See Special:AllMessages for a list of all available messages.

All of the above
Depending on the case, all of the above methods may be combined. For example, MediaWiki messages may be used when available, and when not, a translation table or config file is queried, and if no localization is found there, then a hard-coded English text is used, unless a template parameter overrides it.

Combining several methods can be effective, but the benefits should be weighted against the downsides of the increased complexity, which may cause performance loss and bugs, as well as more difficulty in maintaining the code and recruiting new developers.

Template data
Template parameters are usually stored as a JSON templatedata block inside the template's /doc subpage. This makes it convenient to translate, but when a new parameter is added to a global template, all /doc pages need to be updated in every language. Module:TNT helps with this by automatically generating the templatedata block from a table stored on Commons. Placing the following line into every /doc subpage will use Data:Templatedata/Graph:Lines.tab table to generate all the needed templatedata information in every language. Even if the local community has not translated the full template documentation, they will be able to see all template parameters, centrally updated.

Modules
Global modules do not exist yet and are not possible with current technology. Cross-wiki modules, on the other hand, are possible and do exist. They can be easily maintained using the Synchronizer tool, so that cross-wiki modules are almost as convenient as global modules would be. Furthermore, if global modules ever become possible, then having developed cross-wiki modules would make the transition simple, even trivial.

Templates
Global templates and cross-wiki templates do not exist, and given the way cross-wiki modules work, neither makes much sense. If a template wants to become cross-wiki, usually the best approach is to become a cross-wiki module.

Gadgets
Global gadgets and cross-wiki gadgets both exist. Proveit is an example of a global gadget, while xxx is an example of a cross-wiki gadget.