Translator

Translator is a tool to write language independent applications, i.e. this package provides a means to specify language independent parameterized messages.

See here the Ada specification.

A message is a string with embedded named placeholders.

The syntax of placeholders is Start_Del Name Stop_Del where the start and stop delimiters are single characters.

Name is case-insensitive.

The syntax defines the start and the end of a placeholder so that also parts of a string that do not end with white space can be replaced. The delimiters of a placeholder name may be freely chosen. The following description uses the defaults '{' and '}'.

Any sequence of characters is allowed for a name; it is terminated by the first occurrence of '}'. Each '{' appearing inside a name stands for itself as does each '}' appearing outside a name.

Names can even be nested when different sets of delimiters are used like in {a [nested] name}, although translation then becomes a bit awkward.

A replacement is a mapping of a placeholder name to a value. The delimiting '{' and '}' must not be given with the name.

When a message is translated, the placeholders are replaced by the actual parameter values. Replacements for placeholders that do not occur in the message text are ignored.

Replacement_Error is raised if for a placeholder in a message no replacement is provided.

Syntax_Error is raised if for a start delimiter the corresponding stop delimiter is missing.

Examples:

The message "{verb}ing" with the replacement "verb" => "read" will translate into "reading".

Run the following code fragment to see how the messages are transformed:

  Rules : constant String := "On a square board with @Size@ rows and columns," &
                             " place @Number@ pawns under the following rules: ...";
  Regeln: constant String := "Zu verteilen sind @Number@ Spielsteine auf einem" &
                             " quadratischen Spielbrett mit @Size@ Zeilen und" &
                             " Spalten gemäß folgenden Regeln: ...";
  Put_Line (Translate (Message      => Rules,
                       Placeholders => Replace (Name => "Size"  , by =>  "8") &
                                       Replace (Name => "Number", by => "12"),
                       Start_Delimiter => '@',
                       Stop_Delimiter  => '@'));
  Put_Line (Translate (Message      => Regeln,
                       Placeholders => Replace (Name => "Size"  , by =>  "8") &
                                       Replace (Name => "Number", by => "12"),
                       Start_Delimiter => '@',
                       Stop_Delimiter  => '@'));

For an elaborate example, which uses the Windows registry as a language dictionary that can easily be exchanged by one for another language so that an application can be installed in different languages without recompilation, see FSMedit.

Download:

Download here together with a test program showing various usages.

Last update: 31 January 2002


Contents
Inhaltsverzeichnis