diff --git a/.docs/README.md b/.docs/README.md index 64b708d..f943b1c 100644 --- a/.docs/README.md +++ b/.docs/README.md @@ -15,6 +15,7 @@ - [Database loaders](#database-loaders) - [Doctrine](#doctrine) - [Nette Database](#nette-database) + - [Nextras Dbal](#nextras-dbal) - [Features](#features) - [Wrappers](#wrappers) - [TranslationProviderInterface](#translationproviderinterface) @@ -219,7 +220,7 @@ yml: Symfony\Component\Translation\Loader\YamlFileLoader ### Database loaders -Package includes database loaders for **[Doctrine 2](https://www.doctrine-project.org/)** and **[Nette Database 3](https://doc.nette.org/cs/3.0/database)**. +Package includes database loaders for **[Doctrine 2](https://www.doctrine-project.org/)** and **[Nette Database 3](https://doc.nette.org/cs/3.0/database)** and **[Nextras Dbal](https://nextras.org/dbal/docs/main/)**. #### Doctrine @@ -300,6 +301,25 @@ translation: nettedatabase: Contributte\Translation\Loaders\NetteDatabase ``` +#### Nextras Dbal + +You must create a file with specific format in scanned dirs such as **messages.en_US.nextrasdbal**. All parameters are optional, but the file has to exist. + +```neon +table: "my_table" # if you specify table key, "messages" from file name will be ignored +id: "id" # id column name, default is "id" +locale: "locale" # locale column name, default is "locale" +message: "message" # message column name, default is "message" +``` + +Add loader to translation configuration: + +```neon +translation: + loaders: + nextrasdbal: Contributte\Translation\Loaders\NextrasDbal +``` + DB table example: ```sql diff --git a/composer.json b/composer.json index ce35aa6..591de13 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "nette/database": "^3.1.1", "nette/robot-loader": "^3.4.0|~4.0.0", "nette/tester": "^2.3.1", + "nextras/dbal": "^4.0", "ninjify/nunjuck": "^0.3.0", "ninjify/qa": "^0.13", "phpstan/phpstan": "^1.8", diff --git a/src/Loaders/NextrasDbal.php b/src/Loaders/NextrasDbal.php new file mode 100644 index 0000000..13659ea --- /dev/null +++ b/src/Loaders/NextrasDbal.php @@ -0,0 +1,60 @@ +connection = $connection; + } + + /** + * @inheritdoc + * @throws \Contributte\Translation\Exceptions\InvalidState + * @throws QueryException + */ + protected function getMessages( + array $config, + string $resource, + string $locale, + string $domain + ): array + { + $messages = []; + + $result = $this->connection + ->query( + 'SELECT %column AS `id`, %column AS `locale`, %column AS `message` FROM %table WHERE %column = %s', + $config['id'], + $config['locale'], + $config['message'], + $config['table'], + $config['locale'], + $locale + ) + ->fetchAll(); + + foreach ($result as $row) { + $row = (array) $row; + if (array_key_exists($row['id'], $messages)) { + throw new InvalidState('Id "' . $row['id'] . '" declared twice in "' . $config['table'] . '" table/domain.'); + } + + $messages[$row['id']] = $row['message']; + } + + return $messages; + } + +} diff --git a/tests/Tests/Loaders/NextrasDbalTest.php b/tests/Tests/Loaders/NextrasDbalTest.php new file mode 100644 index 0000000..b9a7cb6 --- /dev/null +++ b/tests/Tests/Loaders/NextrasDbalTest.php @@ -0,0 +1,84 @@ +container->getParameters()['tempDir'], + [ + 'extensions' => [ + 'nextras.dbal' => 'Nextras\Dbal\Bridges\NetteDI\DbalExtension', + ], + 'nextras.dbal' => [ + 'driver' => 'mysqli', + 'host' => '127.0.0.1', + 'port' => 13306, + 'database' => 'test', + 'username' => 'root', + 'password' => '1234', + 'connectionTz' => 'auto-offset', + ], + 'translation' => [ + 'loaders' => [ + 'nextrasdbal' => NextrasDbal::class, + ], + ], + ] + ); + + $this->translator = $container->getByType(ITranslator::class); + $this->connection = $container->getByType(Connection::class); + + try { + $this->connection->connect(); + } catch (ConnectionException $e) { + $this->skip('Database not connected'); + } + } + + public function test01(): void + { + $queries = file_get_contents(__DIR__ . '/../../sql.sql'); + $queries = explode(';', (string) $queries); + $queries = array_filter($queries); + + foreach ($queries as $query) { + $query = trim($query); + if ($query) { + $this->connection->query($query); + } + } + + $this->translator->setLocale('cs_CZ'); + + Assert::same('Ahoj', $this->translator->translate('db_table.hello')); + + $this->translator->setLocale('en_US'); + + Assert::same('Hello', $this->translator->translate('db_table.hello')); + } + +} + +(new NextrasDbalTest($container))->run();