ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ILIAS\Language\ComponentTranslation\ShippedTranslations Class Reference

Compiles the messages of a migrated module into the .mo native gettext serves (see NativeGettext): the shipped state for the build (ShippedTranslationsBuild) and the overlay of local changes (MigratedLanguageFileSync), both via compileCatalog(). More...

+ Collaboration diagram for ILIAS\Language\ComponentTranslation\ShippedTranslations:

Public Member Functions

 __construct (private readonly TranslationMarkupPolicy $markup_policy=new TranslationMarkupPolicy())
 
 compile (string $shipped_po, string $module, ?callable $on_markup_violation=null, ?callable $on_invalid_plural_forms=null)
 The compiled catalog of $module in $shipped_po, with markers (see the class docblock). More...
 
 compileShippedCatalog (TranslationCatalog $shipped, string $module, ?callable $on_markup_violation=null, ?callable $on_invalid_plural_forms=null)
 

Static Public Member Functions

static compileCatalog (TranslationCatalog $source, string $module, bool $mark_identity, ?\Closure $value_of=null, ?callable $on_invalid_plural_forms=null)
 The catalog native gettext serves for $module's messages in $source, see the class docblock. More...
 

Data Fields

const string PLURAL_CONTEXT = 'ilias-plural'
 
const string IDENTITY_CONTEXT = 'ilias-identity'
 

Private Member Functions

 sanitized (string $value, string $key, ?callable $on_markup_violation)
 $value cleaned by the markup policy, $on_markup_violation called for $key if anything was removed. More...
 

Detailed Description

Compiles the messages of a migrated module into the .mo native gettext serves (see NativeGettext): the shipped state for the build (ShippedTranslationsBuild) and the overlay of local changes (MigratedLanguageFileSync), both via compileCatalog().

Layout, so txt() needs one lookup per identifier:

  • every message of the module (see MigratedLanguageFileSync::moduleEntries()) under its identifier WITHOUT context;
  • a plural message additionally with every form under PLURAL_CONTEXT (dngettext() with "<context>\x04<identifier>"), while the entry without context holds its default form (PluralForms::defaultValueOf());
  • with $mark_identity, "IDENTITY_CONTEXT\x04<identifier>" => "1" for every message whose value is its identifier: gettext answers a missing translation with the identifier, this tells "translated as itself" from "not translated" (see TranslationGettextRepository). The charset is always UTF-8; "Plural-Forms" is the rule PluralForms reads from the source (Germanic for a missing or invalid header), so gettext and PluralForms agree. Untranslated messages are left out, fuzzy ones compiled (see TranslationCatalog::toMoString()).

compile() is compileCatalog() of a shipped .po with every value cleaned by TranslationMarkupPolicy::sanitize().

Definition at line 47 of file ShippedTranslations.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Language\ComponentTranslation\ShippedTranslations::__construct ( private readonly TranslationMarkupPolicy  $markup_policy = new TranslationMarkupPolicy())

Definition at line 53 of file ShippedTranslations.php.

55 {
56 }

Member Function Documentation

◆ compile()

ILIAS\Language\ComponentTranslation\ShippedTranslations::compile ( string  $shipped_po,
string  $module,
?callable  $on_markup_violation = null,
?callable  $on_invalid_plural_forms = null 
)

The compiled catalog of $module in $shipped_po, with markers (see the class docblock).

Parameters
(callable(string$identifier, list<string> $violations): void)|null $on_markup_violation called for every cleaned value with what was removed (for a plural form with its form key, see PluralFormKey)
(callable(string$message): void)|null $on_invalid_plural_forms called if there are plural messages but the "Plural-Forms" header is missing or invalid (the Germanic rule is used then)
Returns
array{mo: string, values: array<string, string>} the .mo and the value it serves for each identifier (see compileCatalog())
Exceptions
RuntimeExceptionif $shipped_po cannot be read, parsed or compiled

Definition at line 70 of file ShippedTranslations.php.

75 : array {
76 return $this->compileShippedCatalog(
78 $module,
79 $on_markup_violation,
80 $on_invalid_plural_forms
81 );
82 }
compileShippedCatalog(TranslationCatalog $shipped, string $module, ?callable $on_markup_violation=null, ?callable $on_invalid_plural_forms=null)
if(!isset($arguments[0])) if(count( $arguments) > 3) if(! $remove_from_lang &&isset($arguments[2]) &&!is_dir($arguments[2])) $module

◆ compileCatalog()

static ILIAS\Language\ComponentTranslation\ShippedTranslations::compileCatalog ( TranslationCatalog  $source,
string  $module,
bool  $mark_identity,
?\Closure  $value_of = null,
?callable  $on_invalid_plural_forms = null 
)
static

The catalog native gettext serves for $module's messages in $source, see the class docblock.

Deterministic: the same messages always compile to the same bytes.

Parameters
bool$mark_identitywhether to add the markers of messages translated as their identifier
(

Closure(string $value, string $key): string)|null $value_of what is compiled for a value (every form of a plural message with its form key, see PluralFormKey); default: the value itself

Parameters
(callable(string$message): void)|null $on_invalid_plural_forms see compile()
Returns
array{mo: string, values: array<string, string>} the .mo and the value it serves for each identifier without context (for a plural message the value of its default form), in catalog order
Exceptions
RuntimeExceptionif the messages cannot be compiled (see TranslationCatalog::toMoString())

Definition at line 118 of file ShippedTranslations.php.

124 : array {
125 $catalog = new TranslationCatalog();
126 foreach ($source->getHeaders() as $name => $value) {
127 $catalog->setHeader($name, $value);
128 }
129 // never an empty header: TranslationGettextRepository tells a readable catalog by it, and the C
130 // library takes the charset from it (TranslationCatalog only reads UTF-8)
131 $catalog->setHeader('Content-Type', 'text/plain; charset=UTF-8');
132
133 $plural_forms = null;
134 $values = [];
135 foreach (MigratedLanguageFileSync::moduleEntries($source, $module) as $identifier => $entry) {
136 $identifier = (string) $identifier;
137 if ($entry->isPlural()) {
138 $forms = $entry->getPluralTranslations();
139 foreach ($forms as $form => $form_value) {
140 $forms[$form] = $value_of === null ? $form_value : $value_of($form_value, PluralFormKey::of($identifier, $form));
141 }
142 if (implode('', $forms) === '') {
143 continue;
144 }
145 if ($plural_forms === null) {
146 $plural_forms = PluralForms::fromHeaderOrGermanic($source->getHeader('Plural-Forms'), $on_invalid_plural_forms);
147 $catalog->setHeader('Plural-Forms', $plural_forms->getHeader());
148 }
149 $plural = new TranslationEntry(self::PLURAL_CONTEXT, $identifier);
150 $plural->setPlural((string) $entry->getPluralId(), $forms);
151 $catalog->add($plural);
152 $value = $plural_forms->defaultValueOf($forms);
153 } else {
154 $value = $value_of === null ? $entry->getTranslation() : $value_of($entry->getTranslation(), $identifier);
155 }
156 if ($value === '') {
157 continue;
158 }
159
160 $singular = new TranslationEntry(null, $identifier);
161 $singular->translate($value);
162 $catalog->add($singular);
163 $values[$identifier] = $value;
164 if ($mark_identity && $value === $identifier) {
165 $marker = new TranslationEntry(self::IDENTITY_CONTEXT, $identifier);
166 $marker->translate('1');
167 $catalog->add($marker);
168 }
169 }
170
171 return ['mo' => $catalog->toMoString(), 'values' => $values];
172 }
An in-memory gettext catalog (header block plus messages) and the only place of the component that re...
static moduleEntries(TranslationCatalog $catalog, string $module)
The entries of $catalog that belong to $module: those with the module as context (msgctxt) and those ...
static of(string $identifier, int $form)
static fromHeaderOrGermanic(?string $header, ?callable $on_invalid=null)
fromHeader(), or the Germanic rule (GERMANIC_HEADER) if $header is missing or invalid - reported thro...

References ILIAS\Language\ComponentTranslation\Catalog\TranslationCatalog\setHeader().

+ Here is the call graph for this function:

◆ compileShippedCatalog()

ILIAS\Language\ComponentTranslation\ShippedTranslations::compileShippedCatalog ( TranslationCatalog  $shipped,
string  $module,
?callable  $on_markup_violation = null,
?callable  $on_invalid_plural_forms = null 
)
Parameters
(callable(string$identifier, list<string> $violations): void)|null $on_markup_violation see compile()
(callable(string$message): void)|null $on_invalid_plural_forms see compile()
Returns
array{mo: string, values: array<string, string>} see compile()
Exceptions
RuntimeExceptionif the messages cannot be compiled

Definition at line 90 of file ShippedTranslations.php.

95 : array {
97 $shipped,
98 $module,
99 true,
100 fn(string $value, string $key): string => $this->sanitized($value, $key, $on_markup_violation),
101 $on_invalid_plural_forms
102 );
103 }
static compileCatalog(TranslationCatalog $source, string $module, bool $mark_identity, ?\Closure $value_of=null, ?callable $on_invalid_plural_forms=null)
The catalog native gettext serves for $module's messages in $source, see the class docblock.
sanitized(string $value, string $key, ?callable $on_markup_violation)
$value cleaned by the markup policy, $on_markup_violation called for $key if anything was removed.

◆ sanitized()

ILIAS\Language\ComponentTranslation\ShippedTranslations::sanitized ( string  $value,
string  $key,
?callable  $on_markup_violation 
)
private

$value cleaned by the markup policy, $on_markup_violation called for $key if anything was removed.

Parameters
(callable(string$identifier, list<string> $violations): void)|null $on_markup_violation

Definition at line 179 of file ShippedTranslations.php.

179 : string
180 {
181 $violations = $this->markup_policy->findViolations($value);
182 if ($violations === []) {
183 return $value;
184 }
185 if ($on_markup_violation !== null) {
186 $on_markup_violation($key, $violations);
187 }
188
189 return $this->markup_policy->sanitize($value);
190 }

Field Documentation

◆ IDENTITY_CONTEXT

const string ILIAS\Language\ComponentTranslation\ShippedTranslations::IDENTITY_CONTEXT = 'ilias-identity'

◆ PLURAL_CONTEXT

const string ILIAS\Language\ComponentTranslation\ShippedTranslations::PLURAL_CONTEXT = 'ilias-plural'

The documentation for this class was generated from the following file: