1. Go to this page and download the library: Download someson/phalcon-i18n library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
someson / phalcon-i18n example snippets
// component using a singleton pattern, so we can instantiate it before the framework itself
// or wrap it into some global function
$t = \Phalcon\I18n\Translator::instance();
// using the "de" directory, "en" by default
$t->setLang('de');
// equal to "global:a", hence "global" is a default scope
echo $t->_('a');
// placeholder "key" replaced through "value"
echo $t->_('b', ['key' => 'value']);
// nested key
echo $t->_('c.d.e', ['key' => 'value']);
// nested key from the "api" scope (filename === scope, if files used)
echo $t->_('api:c.d.e', ['key' => 'value']);
use \Phalcon\I18n\Translator;
if (! function_exists('__')) {
function __(string $key, array $params = [], bool $pluralize = true): string {
return $key ? Translator::instance()->_($key, $params, $pluralize) : '[TRANSLATION ERROR]';
}
}
$translation = __('a.b.c');
return [
'defaultLang' => 'en',
'defaultScope' => 'global',
// loads data from chosen source (e.g. Json) by chosen loader (e.g. Files)
// can be e.g. "Mysql" by "Database" (feel free to implement)
'loader' => [
'className' => \Phalcon\I18n\Loader\Files::class,
'arguments' => ['path' => $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'locale'],
],
// reads the source and translates it into chosen type of handler (@see key "handler")
'adapter' => [
'className' => \Phalcon\I18n\Adapter\Json::class,
],
// implements \Phalcon\Translate\AdapterInterface
// returns an object of all translations of the specific language
// provides functionality for placeholder replacing
'handler' => [
'options' => [
'flatten' => ['shift' => 1],
],
],
// replaces user-defined (or '%' by default) placeholders
'interpolator' => [
'className' => \Phalcon\I18n\Interpolator\AssocArray::class,
'arguments' => ['{{', '}}'],
],
// bool only
'collectMissingTranslations' => true,
// - false
// - sprintf pattern e.g. [# %s #]
// - \Phalcon\I18n\Interfaces\DecoratorInterface object
'decorateMissingTranslations' => new \Phalcon\I18n\Decorator\HtmlCode,
];