Giter VIP home page Giter VIP logo

translator-message-db's Introduction

Yii Translator DB Message Storage


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

Latest Stable Version Total Downloads codecov Mutation testing badge static analysis type-coverage

Supported databases

Packages PHP Versions CI-Actions
[db-mssql] 8.0 - 8.2 2017 - 2022 mssql
[db-mysql/mariadb] 8.0 - 8.2 5.7-8.0/10.4-10.10 mysql
[db-oracle] 8.0 - 8.2 11C - 21C oracle
[db-pgsql] 8.0 - 8.2 9.0 - 15.0 pgsql
[db-sqlite] 8.0 - 8.2 3:latest sqlite

Requirements

  • PHP 8.0 or higher.
  • json PHP extension.

Installation

The package could be installed with composer:

composer require yiisoft/translator-message-db --prefer-dist

Create database connection

For more information see yiisoft/db.

Database Preparing

Package provides two way for preparing database:

  1. Raw SQL. You can use it with the migration package used in your application.

  2. DbSchemaManager for ensureTables(), ensureNoTables() methods for translator tables (by default {{%yii_source_message}} and {{%yii_message}}).

Configuration

Quick start

In case you use yiisoft/config, you will get configuration automatically. If not, the following DI container configuration is necessary:

<?php

declare(strict_types=1);

use Yiisoft\Translator\MessageReaderInterface;
use Yiisoft\Translator\Message\Db\MessageSource;

return [
    MessageReaderInterface::class => [
        'class' => MessageSource::class,
    ],
];

General usage

Create of instance of MessageSource

/** @var \Yiisoft\Db\Connection\ConnectionInterface $db */
$messageSource = new \Yiisoft\Translator\Message\Db\MessageSource($db);

Create of instance of MessageSource with caching

/** @var \Yiisoft\Db\Connection\ConnectionInterface $db */
/** @var \Yiisoft\Cache\CacheInterface $cache */
$cacheDuration = 7200; // The TTL of this value. If set to null, default value is used - 3600
$messageSource = new \Yiisoft\Translator\Message\Db\MessageSource($db, $cache, $cacheDuration);

Read message without yiisoft/translator package

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

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

Writing messages from array to storage

/** 
 * @var \Yiisoft\Translator\Message\Db\MessageSource $messageSource
 */
$category = 'messageCategory';
$language = 'de-DE';
$data = [
    'test.id1' => [
        'message' => 'Nachricht 1', // translated string
        'comment' => 'Comment for message 1', // is optional parameter for save extra metadata
    ],
    'test.id2' => [
        'message' => 'Nachricht 2',
    ],
    'test.id3' => [
        'message' => 'Nachricht 3',
    ],
];

$messageSource->write($category, $language, $data);

Support

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

Testing

Check the testing instructions to learn about testing.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

License

The Yii Translator DB 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.

translator-message-db's People

Contributors

darkdef avatar dependabot[bot] avatar devanych avatar fantom409 avatar rustamwin avatar samdark avatar sankaest avatar terabytesoftw avatar thenotsoft avatar viktorprogger avatar vjik avatar xepozz avatar

Stargazers

 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

translator-message-db's Issues

Reset cache

Need implement reset for translations cache

i18n migration collation. Make it utf8_bin instead of utf8_unicode_ci

It's more of a suggestion/question.

When you create tables source_mesage and message with Yii migration the default collation used is utf8_unicode_ci

if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
  1. MySQL treats strings as accent insensitive. "Revolución" is same as "Revolucion".
  2. Now in Yii2 isset in array is used to find translations.
if (isset($this->_messages[$key][$message]) && $this->_messages[$key][$message] !== '') {
            return $this->_messages[$key][$message];
        }

Inconsistency, what happened to me is that in my back office there is a translation for "Revolución", cause MySQL returned results for it. But in front office, Yii fails to source language cause of php isset.

My question/proposition is to change the collation to utf8_bin on source_message.message. So in the back office, there won't be any misleading translation loaded.

Additional info

Q A
Yii version 2.0.12?
PHP version
Operating system

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.