Open main menu

Справка:Расширение:Перевод/Вставки

This page is a translated version of the page Help:Extension:Translate/Insertables and the translation is 18% complete.
Other languages:
Deutsch • ‎English • ‎français • ‎čeština • ‎русский • ‎中文 • ‎日本語

Переводимые строки часто содержат разметку, которая должна быть сохранена в переводе в неизменном виде. Вводить такую разметку может быть медленно и трудно, поскольку она содержит специальные символы и символьные коды, которые можно перепутать с переводимым текстом. Insertable is a piece of markup that is presented to the translator as a button or other interface element. Clicking the button inserts the piece of markup into the translation to the current cursor position.

Each message group can have one InsertablesSuggester. This class is responsible for generating a list of Insertables. Each Insertable has three parts:

  1. What to display to the user
  2. What is inserted before cursor position in the translation
  3. What is inserted after cursor position in the translation

Translate extension comes with MediaWikiInsertablesSuggester built-in. Suggesters for other type of content can be found in the translatewiki.net git repository.

Пользовательский интерфейс

 
Insertables show in the bottom of the translation text area. They provide quick access for inserting pieces of mark-up that does not vary per language.

Пример кода

Here is an example about adding insertables support for existing file based message group: FreeCol. The YAML file has been trimmed for brevity. The newly added lines are highlighted.

FreeCol.yaml:

---
BASIC:
  id: out-freecol
  label: FreeCol
  description: "A strategy game"
  namespace: NS_FREECOL
  class: FileBasedMessageGroup
 
FILES:
  class: JavaFFS
  sourcePattern: %GROUPROOT%/freecol/data/strings/FreeColMessages_%CODE%.properties
  definitionFile: %GROUPROOT%/freecol/data/strings/FreeColMessages.properties
  targetPattern: freecol/data/strings/FreeColMessages_%CODE%.properties

CHECKER:
  class: FreeColMessageChecker
  checks:
    - FreeColEscapesCheck
    - FreeColVariablesCheck

INSERTABLES:
  class: FreeColInsertablesSuggester

AUTOLOAD:
  FreeColMessageChecker: Checker.php
  FreeColInsertablesSuggester: Suggester.php

Suggester.php:

class FreecolInsertablesSuggester {
	public function getInsertables( $text ) {
		$insertables = array();

		$matches = array();
		// Find variables of format %name%
		// This is the same regular expression as in Checker.php
		preg_match_all( '/%[a-zA-Z_]+%/', $text, $matches, PREG_SET_ORDER );
		$new = array_map( function( $match ) {
			// $match[0] is full match, and we don't have any submatches here
			// Since we want the cursor to be at the end of the insertion, we put the whole thing into the "pre" field.
			return new Insertable( $match[0], $match[0] );
		}, $matches );

		return $insertables;
	}
}

The parameters to Insertable are:

class Insertable {
        /**
         * @param string $display What to show to the user
         * @param string $pre What to insert before selection
         * @param string $post What to insert after selection
         */
        public function __construct( $display, $pre = '', $post = '' ) {
                $this->display = $display;
                $this->pre = $pre;
                $this->post = $post;
        }
[...]
}