Extended translation module

Overview

Multi-lingual Support

MIT License CircleCI codecov Packagist Version (including pre-releases) Made in Ukraine Russian Warship Go Fuck Yourself

Extending Phalcon Framework v5 Translations Module

Example

e.g. login.json File:

{
    "form": {
        "label": {
            "identity": "Benutzername",
            "password": "Passwort",
            "rememberMe": "Ich möchte angemeldet bleiben"
        },
        "placeholder": {
            "identity": "Bitte geben Sie ihren Benutzernamen ein",
            "password": "Bitte geben Sie ihr Passwort ein"
        },
        "button": "Anmelden"
    },
    "title": {
        "h1": "Main Title",
        "h2": "some subtitle"
    }
}

translating path like login:form.label.identity returns Benutzername

which start with login: means file name and the rest of it is a pure json path.

Usage

1. Simple usage

// 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']);

2. Advanced usage

in any bootstrap file (i.e. index.php) define:

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]';
    }
}

inside your code:

$translation = __('a.b.c');

or in any view:

<h1><?= __('a.b.c') ?></h1>
<h1>{{ __('a.b.c') }}</h1>

Configure

default config \Phalcon\I18n\Config\Default.php:

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,
];

you may want to override it with your own config (by default used in config container having i18n scope):

return [
    // ...

    'i18n' => [
        'loader' => [
            'arguments' => ['path' => '/my/own/path/to/locale/'],
        ],
        'interpolator' => [
            'arguments' => ['[[', ']]'],
        ],
        'collectMissingTranslations' => false,
        'decorateMissingTranslations' => '[# %s #]',
    ],

    // ...
];

Running Tests

Codeception used

To run tests, run the following command:

$ docker-compose exec php-service ./vendor/bin/codecept run unit [-vv] 
$ docker-compose exec php-service ./vendor/bin/codecept run --coverage
Codeception PHP Testing Framework v4.1.31 https://helpukrainewin.org
Powered by PHPUnit 9.5.20 #StandWithUkraine

Unit Tests (29) ----------------------------------------------------------
✔ AdapterTest: Json found and initialized (0.23s)
✔ AdapterTest: Json may throw exceptions (0.10s)
✔ ConfigTest: Must be functional with config service (0.11s)
✔ ConfigTest: Must be functionable without config service (0.02s)
✔ ConfigTest: Must be functionable with wrong config (0.01s)
✔ DecoratorTest: No decoration (0.02s)
✔ DecoratorTest: Decorate as text pattern (0.02s)
✔ DecoratorTest: Decorate as html (0.02s)
✔ HandlerTest: Check keys shifting | #0 (0.01s)
✔ HandlerTest: Check keys shifting | #1 (0.01s)
✔ HandlerTest: Check keys shifting | #2 (0.01s)
✔ InterpolatorTest: Should handle default placeholders (0.00s)
✔ InterpolatorTest: Should handle custom placeholders (0.00s)
✔ LoaderTest: Files loader | #0 (0.08s)
✔ LoaderTest: Files loader | #1 (0.02s)
✔ TranslatorTest: Fallback loaded (0.03s)
✔ TranslatorTest: Wrong fallback lang defined (0.01s)
✔ TranslatorTest: Default instance (0.01s)
✔ TranslatorTest: Change language (0.00s)
✔ TranslatorTest: Change scope (0.00s)
✔ TranslatorTest: Changed scope should return a new collection (0.02s)
✔ TranslatorTest: Check if translation exists (0.03s)
✔ TranslatorTest: Plural (0.02s)
✔ TranslatorTest: Context (0.02s)
✔ TranslatorTest: Missing translations (0.02s)
✔ TranslatorTest: Simple translation without parameters (0.02s)
✔ TranslatorTest: Simple translation with parameters (0.02s)
✔ TranslatorTest: Translation with deeper level (0.02s)
--------------------------------------------------------------------------


Time: 00:09.936, Memory: 26.00 MB

OK (28 tests, 52 assertions)


Code Coverage Report Summary:
  Classes: 75.00% (6/8)
  Methods: 93.10% (27/29)
  Lines:   98.59% (140/142)

For code coverage info run

$ docker-compose exec php-service ./vendor/bin/codecept run --coverage --coverage-html

and open tests/_output/coverage/index.html in your browser

Static analyzer

$ docker-compose exec php-service ./vendor/bin/phpstan analyse src --level max

You might also like...
Magento2 Spanish (Argentina) language pack build from Crowdin community translation tool.

Magento2-language-es_ar Magento2 Spanish (Argentina) language pack build from Crowdin community translation tool. Paquete de idioma de Español (Argent

Issue trackers, feature requests, and translation for all of my NamelessMC products
Issue trackers, feature requests, and translation for all of my NamelessMC products

NamelessMC Products In this repository you can find current issues for each of my NamelessMC products, progress on solving them, feature requests, and

Migrations module for ProcessWire

ProcessDbMigrate Introduction This module is designed to ease the problem of migrating database changes from one PW environment to another.

WHMCS Payment Gateway Module for Coinify

vrcoinify WHMCS Payment Gateway Module for Coinify Installing guide You should copy all contents from module folder to your WHMCS application folder u

The swiss army knife for Magento developers, sysadmins and devops. The tool provides a huge set of well tested command line commands which save hours of work time. All commands are extendable by a module API.

netz98 magerun CLI tools for Magento 2 The n98 magerun cli tools provides some handy tools to work with Magento from command line. Build Status Latest

SilverStripe Garbage Collection Module

SilverStripe Module for defining and processing Garbage Collection on SilverStripe Applications.

WHMCS Automation Module For AWS EC2 Instances.
WHMCS Automation Module For AWS EC2 Instances.

使用方法 把AWSEC2目录直接扔到 WHMCS/modules/servers 下即可 自定义字段 cloudinit (文本框 textarea 在订单页面显示) pem (文本框 textarea 仅管理员可见) data (文本框 textarea 仅管理员可见) 特性 动态IP (关机再开

Akaunting module to use employee as a customer

Associate Employee to customer App for Akaunting to associate employee to customer. With this app contacts with the employee type can be used as custo

Manifest is a ProcessWire module that bridges between Twig and Webpack.

Manifest is a ProcessWire module that bridges between Twig and Webpack.

Releases(v1.3.3)
Owner
someson
Слава Україні!
someson
This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every translatable field.

Autotranslate This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every

null 4 Jan 3, 2022
Library download currency rate and save in database, It's designed to be extended by any available data source.

Library download currency rate and save in database, It's designed to be extended by any available data source.

Flexmind. Krzysztof Bielecki 2 Oct 6, 2021
Converts any PocketMine-MP 3.0 extended blocks into PM4 native blocks!

ExtendedBlocksConverter Converts any PocketMine-MP 3.0 extended blocks into PM4 native blocks! Yes, you heard right, this plugin can convert any lefto

Covered123 6 Jun 4, 2022
The Cache component provides an extended PSR-6 implementation for adding cache to your applications.

Symfony PSR-6 implementation for caching The Cache component provides an extended PSR-6 implementation for adding cache to your applications. It is de

Symfony 3.8k Jan 3, 2023
QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific.

QuidPHP/Main is a PHP library that provides a set of base objects and collections that can be extended to build something more specific. It is part of the QuidPHP package and can also be used standalone.

QuidPHP 4 Jul 2, 2022
Magento 2 Module Experius Page Not Found 404. This module saves all 404 url to a database table

Magento 2 Module Experius Page Not Found 404 This module saves all 404 urls to a database table. Adds an admin grid with 404s It includes a count so y

Experius 28 Dec 9, 2022
Magento2 Turkish Translation / Magento2 Türkçe Çevirisi

Magento 2 Türkçe Dil Paketi Magento2 için Türkçe Dil Paketi Magento2 içinde standart olarak gelen tüm tercüme dosyaları derlenmiş ve bu paket oluşturu

Hidayet Ok 28 Dec 13, 2022