Add Internationalization

- Add Utils/L10n class
- Add translator functions to PHP Renderer
- Refactor web controllers to prevent duplicated code
- Add locale middleware
- Add translation file loading
- Add i18n settings
This commit is contained in:
Hypolite Petovan 2018-11-15 23:59:00 -05:00
commit 5b7bb030de
21 changed files with 537 additions and 245 deletions

View file

@ -6,6 +6,21 @@ namespace Friendica\Directory\Views;
* Zend-Escaper wrapper for Slim PHP Renderer
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*
* @method string escapeHtml(string $value)
* @method string escapeHtmlAttr(string $value)
* @method string escapeCss(string $value)
* @method string escapeJs(string $value)
* @method string escapeUrl(string $value)
* @method string noop(string $original)
* @method string gettext(string $original)
* @method string ngettext(string $original, string $plural, string $value)
* @method string dngettext(string $domain, string $original, string $plural, string $value)
* @method string npgettext(string $context, string $original, string $plural, string $value)
* @method string pgettext(string $context, string $original)
* @method string dgettext(string $domain, string $original)
* @method string dpgettext(string $domain, string $context, string $original)
* @method string dnpgettext(string $domain, string $context, string $original, string $plural, string $value)
*/
class PhpRenderer extends \Slim\Views\PhpRenderer
{
@ -14,13 +29,13 @@ class PhpRenderer extends \Slim\Views\PhpRenderer
*/
private $escaper;
/**
* @var \Friendica\Directory\Content\L10n
* @var \Gettext\TranslatorInterface
*/
private $l10n;
public function __construct(
\Zend\Escaper\Escaper $escaper,
\Friendica\Directory\Content\L10n $l10n,
\Gettext\TranslatorInterface $l10n,
string $templatePath = "",
array $attributes = array()
)
@ -36,28 +51,106 @@ class PhpRenderer extends \Slim\Views\PhpRenderer
return $this->escapeHtml($value);
}
public function escapeHtml(string $value): string
public function __call($name, $arguments)
{
return $this->escaper->escapeHtml($value);
if (method_exists($this->escaper, $name)) {
return $this->escaper->$name(...$arguments);
} elseif (method_exists($this->l10n, $name)) {
return $this->l10n->$name(...$arguments);
} else {
throw new \Exception('Unknown PhpRendere magic method: ' . $name);
}
}
public function escapeCss(string $value): string
/**
* Echoes the translation of a string.
*
* Loose copy of Gettext/gettext global __() function
*
* Usages:
* - $this->__('Label')
* - $this->__('Label %s', $value)
*
* @param $original
* @param array $args
*
* @return string
*/
public function __($original, ...$args)
{
return $this->escaper->escapeCss($value);
$text = $this->l10n->gettext($original);
if (!count($args)) {
return $text;
}
return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args);
}
public function escapeJs(string $value): string
/**
* Returns the translation of a string in a specific context.
*
* @param string $context
* @param string $original
*
* @param array $args
* @return string
*/
function p__($context, $original, ...$args)
{
return $this->escaper->escapeJs($value);
$text = $this->l10n->pgettext($context, $original);
if (!count($args)) {
return $text;
}
return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args);
}
public function escapeHtmlAttr(string $value): string
/**
* Returns the translation of a string in a specific domain.
*
* @param string $domain
* @param string $original
*
* @param array $args
* @return string
*/
function d__($domain, $original, ...$args)
{
return $this->escaper->escapeHtmlAttr($value);
$text = $this->l10n->dgettext($domain, $original);
if (!count($args)) {
return $text;
}
return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args);
}
public function escapeUrl(string $value): string
/**
* Echoes the singular/plural translation of a string.
*
* Loose copy of Gettext/gettext global n__() function
*
* Usages:
* - $this->n__('Label', 'Labels', 3)
* - $this->n__('%d Label for %s', '%d Labels for %s', 3, $value)
*
* @param string $original
* @param string $plural
* @param string $count
* @param array $args
*
* @return string
*/
function n__($original, $plural, $count, ...$args)
{
return $this->escaper->escapeUrl($value);
$text = $this->l10n->ngettext($original, $plural, $count);
array_unshift($args, $count);
return is_array($args[1]) ? strtr($text, $args[1]) : vsprintf($text, $args);
}
}