Skip to content

Commit

Permalink
absolute translation / prefixed translation support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleš committed Mar 29, 2019
1 parent db44b94 commit 9d49715
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
42 changes: 40 additions & 2 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @property-read bool $debug
* @property-read Translette\Translation\Tracy\Panel|null $tracyPanel
* @property array|null $localesWhitelist
* @property string|null $domain
* @property-read array $availableLocales
* @property string|null $locale
*
Expand Down Expand Up @@ -52,9 +53,11 @@ class Translator extends Symfony\Component\Translation\Translator implements Net
/** @var array|null */
private $localesWhitelist;

/** @var array */
private $resourcesLocales = [];
/** @var string|null */
private $domain;

/** @var array @internal */
private $resourcesLocales = [];

/**
* @param Translette\Translation\LocaleResolver $localeResolver
Expand Down Expand Up @@ -153,6 +156,25 @@ public function setLocalesWhitelist(?array $whitelist): self
}


/**
* @return string|null
*/
public function getDomain(): ?string
{
return $this->domain;
}


/**
* @param null|string $string
* @return self
*/
public function setDomain(?string $string): self
{
$this->domain = $string;
return $this;
}

/**
* @return array
*/
Expand Down Expand Up @@ -243,6 +265,22 @@ public function translate($message, ...$parameters): string // @comment
}


/**
* {@inheritdoc}
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if (is_string($domain) && Nette\Utils\Strings::startsWith($domain, '//')) {
$domain = Nette\Utils\Strings::substring($domain, 2);

} elseif ($this->domain !== null) {
$domain = $this->domain;
}

return parent::trans($id, $parameters, $domain, $locale);
}


/**
* {@inheritdoc}
*/
Expand Down
8 changes: 4 additions & 4 deletions src/latte/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function macroTranslate(Latte\MacroNode $node, Latte\PhpWriter $writer)
// return $writer->write('echo %modify(($this->filters->translate)(%node.args))');

if (Translette\Translation\Helpers::macroWithoutParameters($node)) {
return $writer->write('echo %modify(call_user_func($this->filters->translate, (isset($_translatorDomain) ? $_translatorDomain : "") . %node.word))');
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word))');
}

return $writer->write('echo %modify(call_user_func($this->filters->translate, (isset($_translatorDomain) ? $_translatorDomain : "") . %node.word, %node.args))');
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word, %node.args))');
}
}

Expand All @@ -70,15 +70,15 @@ public function macroDomain(Latte\MacroNode $node, Latte\PhpWriter $writer)
{
if ($node->closing) {
if ($node->content !== null && $node->content !== '') {
return $writer->write('unset($_translatorDomain);');
return $writer->write('$this->global->translator->domain = null;');
}

} else {
if ($node->args === '') {
throw new Latte\CompileException('Expected message domain, none given.');
}

return $writer->write('$_translatorDomain = %node.word . ".";');
return $writer->write('$this->global->translator->domain = %node.word;');
}
}
}

0 comments on commit 9d49715

Please # to comment.