Coder Social home page Coder Social logo

yiisoft / translator-message-gettext Goto Github PK

View Code? Open in Web Editor NEW
13.0 17.0 9.0 113 KB

Yii i18n gettext message storage

Home Page: https://www.yiiframework.com/

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%
yii3 i18n gettext hacktoberfest

translator-message-gettext's Introduction

Yii Translator gettext Message Storage


Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

The package provides message storage backend based on gettext file format to be used with yiisoft/translator package.

Requirements

  • PHP 8.0 or higher.
  • gettext PHP extension.

Installation

The package could be installed with composer:

composer require yiisoft/translator-message-gettext

Attention

When using gettext, locale depends on your OS requirement:

  • for Windows 'locale' => 'en-US.UTF-8'
  • for Linux 'locale' => 'en_US.UTF-8'

General usage

The package is meant to be used with yiisoft/translator:

use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\Message\Gettext\MessageSource;

$categorySource = new CategorySource(
    'my-category',
    new MessageSource('/path/to/messages'),
);

The examples below are about using it separately.

Create of instance of MessageSource

/** @var string $path - path to your gettext storage */
$messageSource = new \Yiisoft\Translator\Message\Gettext\MessageSource($path);

Read message without yiisoft/translator package

/** 
 * @var \Yiisoft\Translator\Message\Gettext\MessageSource $messageSource
 * @var ?string $translatedString
 */
$id = 'messageIdentificator';
$category = 'messages';
$language = 'de-DE';

$translatedString = $messageSource->getMessage($id, $category, $language);

You can create your translations in .mo format with use third-party software (for example Poedit)

Recommended structure to your gettext storage:

-- path_to_your_storage
  -- de_DE
    -- LC_MESSAGES
        -- messages.mo

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Translator gettext Message Storage is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

translator-message-gettext's People

Contributors

bitwire avatar darkdef avatar dependabot-preview[bot] avatar dependabot[bot] avatar devanych avatar fantom409 avatar filsh avatar machour avatar mister-42 avatar samdark avatar terabytesoftw avatar viktorprogger avatar vjik avatar xepozz avatar yiiliveext avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

translator-message-gettext's Issues

Incorrect handling of parameters

What steps will reproduce the problem?

echo $t->translate('Sign in successful - {date}', ['date' => date('Y-m-d G:i:s')], 'user');

What is the expected result?

Sign in successful - 2021-01-16 13:37:42

What do you get instead?

TypeError: dngettext(): Argument #4 ($count) must be of type int, string given in /yii3/vendor/yiisoft/translator-message-gettext/src/MessageSource.php:35
Stack trace:
#0 /yii3/vendor/yiisoft/translator-message-gettext/src/MessageSource.php(35): dngettext()
#1 /yii3/vendor/yiisoft/translator/src/Category.php(42): Yiisoft\Translator\Message\Gettext\MessageSource->getMessage()
#2 /yii3/vendor/yiisoft/translator/src/Translator.php(92): Yiisoft\Translator\Category->getMessage()
#3 /yii3/mister42/Control/site/index.php(19): Yiisoft\Translator\Translator->translate()
#4 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(13): require('...')
#5 /yii3/vendor/yiisoft/view/src/PhpTemplateRenderer.php(20): Yiisoft\View\PhpTemplateRenderer->Yiisoft\View\{closure}()
#6 /yii3/vendor/yiisoft/view/src/View.php(361): Yiisoft\View\PhpTemplateRenderer->render()
#7 /yii3/vendor/yiisoft/view/src/View.php(264): Yiisoft\View\View->renderFile()
#8 /yii3/vendor/yiisoft/yii-view/src/ViewRenderer.php(193): Yiisoft\View\View->render()
#9 /yii3/vendor/yiisoft/yii-view/src/ViewRenderer.php(74): Yiisoft\Yii\View\ViewRenderer->renderProxy()
#10 /yii3/vendor/yiisoft/data-response/src/DataResponse.php(163): Yiisoft\Yii\View\ViewRenderer->Yiisoft\Yii\View\{closure}()
#11 /yii3/vendor/yiisoft/data-response/src/Formatter/HtmlDataResponseFormatter.php(29): Yiisoft\DataResponse\DataResponse->getData()
#12 /yii3/vendor/yiisoft/data-response/src/DataResponse.php(179): Yiisoft\DataResponse\Formatter\HtmlDataResponseFormatter->format()
#13 /yii3/vendor/yiisoft/data-response/src/DataResponse.php(40): Yiisoft\DataResponse\DataResponse->formatResponse()
#14 /yii3/vendor/yiisoft/yii-web/src/SapiEmitter.php(101): Yiisoft\DataResponse\DataResponse->getBody()
#15 /yii3/vendor/yiisoft/yii-web/src/SapiEmitter.php(40): Yiisoft\Yii\Web\SapiEmitter->shouldOutputBody()
#16 /yii3/webroot/index.php(59): Yiisoft\Yii\Web\SapiEmitter->emit()
#17 {main}

Additional info

Q A
Version dev
PHP version 8.0.1
Operating system Debian 10

Add support for gettext plural forms

While ICU i18n implementation is very nice and featured, there are some translators that would like to work with gettext plurals implementation, that looks like:

msgid "%d Comment"
msgid_plural "%d Comments"
msgstr[0] "%d Komentarz"
msgstr[1] "%d Komentarze"
msgstr[2] "%d Komentarzy"

So translators will see something similar in Poedit:

Poedit Plurals

So the question: "Is it possible to implement?".

Gettext naming convention

Issue : Yii gettext path for mo files is different from original gettext

Server configuration : Debian 8, PHP 7.18, Yii 2.0.12

Default gettext naming on debian is

../locale/fr_FR/LC_MESSAGES/messages.mo
../locale/fr_FR/LC_MESSAGES/messages.po

Gettext path with Yii
../locale/fr-FR/messages.mo

If we want compatibility we need to correct 2 problems :

  • replace the '-' with '_' in $language
  • add the directory /LC_MESSAGES

We can modify : yii2\i18n\GettextMessageSource.php

    protected function getMessageFilePath($language)
    {
//      $messageFile = Yii::getAlias($this->basePath) . '/' . $language . '/' . $this->catalog;
        $messageFile = Yii::getAlias($this->basePath) . '/' . str_replace('-', '_', $language) . '/LC_MESSAGES/' . $this->catalog; 
        if ($this->useMoFile) {
            $messageFile .= self::MO_FILE_EXT;
        } else {
            $messageFile .= self::PO_FILE_EXT;
        }
        return $messageFile;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.