Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
"issues": "https://github.com/zerospam/laravel-gettext/issues"
},
"require": {
"php": ">=7.2",
"laravel/framework": "^6.0 || ^7.0 || ^8.0",
"laravel/helpers": "^1.1"
"php": "^8.0|^8.1|^8.2|^8.3",
"laravel/framework": "^9.0 || ^10.0 || ^11.0 || ^12.0",
"laravel/helpers": "^1.6"
},
"require-dev": {
"mockery/mockery": "dev-master",
"phpunit/phpunit": "~7.0 || ^8.5 || ^9",
"squizlabs/php_codesniffer" : "1.5.*",
"laravel/laravel": "^6.0 || ^7.0 || ^8.0",
"php-coveralls/php-coveralls": "^2.1"
"mockery/mockery": "^1.6",
"phpunit/phpunit": "^9.5 || ^10.0",
"squizlabs/php_codesniffer": "^3.7",
"laravel/laravel": "^9.0 || ^10.0 || ^11.0 || ^12.0",
"php-coveralls/php-coveralls": "^2.5"
},
"autoload": {
"psr-0": {
Expand All @@ -43,7 +43,8 @@
"src/Xinax/LaravelGettext/Support/helpers.php"
]
},
"minimum-stability": "stable",
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"laravel": {
"providers": [
Expand Down
22 changes: 16 additions & 6 deletions src/Xinax/LaravelGettext/LaravelGettext.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

declare(strict_types=1);

namespace Xinax\LaravelGettext;

use Xinax\LaravelGettext\Composers\LanguageSelector;
use Xinax\LaravelGettext\Translators\TranslatorInterface;
use Xinax\LaravelGettext\Exceptions\MissingPhpGettextModuleException;
use Xinax\LaravelGettext\Exceptions\LocaleNotSupportedException;

class LaravelGettext
{
Expand All @@ -14,9 +18,14 @@ class LaravelGettext
*/
protected $translator;

/**
* @var string
*/
protected $encoding = 'UTF-8';

/**
* @param TranslatorInterface $gettext
* @throws Exceptions\MissingPhpGettextModuleException
* @throws MissingPhpGettextModuleException
*/
public function __construct(TranslatorInterface $gettext)
{
Expand Down Expand Up @@ -60,7 +69,7 @@ public function getLocale()
*
* @param string $locale
* @return $this
* @throws Exceptions\LocaleNotSupportedException
* @throws LocaleNotSupportedException
* @throws \Exception
*/
public function setLocale($locale)
Expand Down Expand Up @@ -130,7 +139,7 @@ public function getDomain()
/**
* Translates a message with the current handler
*
* @param $message
* @param string $message
* @return string
*/
public function translate($message)
Expand All @@ -141,9 +150,9 @@ public function translate($message)
/**
* Translates a plural string with the current handler
*
* @param $singular
* @param $plural
* @param $count
* @param string $singular
* @param string $plural
* @param int $count
* @return string
*/
public function translatePlural($singular, $plural, $count)
Expand Down Expand Up @@ -186,6 +195,7 @@ public function getSupportedLocales()
/**
* Indicates if given locale is supported
*
* @param string $locale
* @return bool
*/
public function isLocaleSupported($locale)
Expand Down
33 changes: 10 additions & 23 deletions src/Xinax/LaravelGettext/LaravelGettextServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ class LaravelGettextServiceProvider extends ServiceProvider

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->publishes([
__DIR__ . '/../../config/config.php' => config_path('laravel-gettext.php')
], 'config');

}

/**
* Register the service provider.
*
* @return mixed
*/
public function register()
public function register(): void
{
$configuration = ConfigManager::create();

Expand Down Expand Up @@ -97,32 +92,24 @@ public function register()
/**
* Register commands
*/
protected function registerCommands()
protected function registerCommands(): void
{
// Package commands
$this->app->bind('xinax::gettext.create', function ($app) {
return new Commands\GettextCreate();
});

$this->app->bind('xinax::gettext.update', function ($app) {
return new Commands\GettextUpdate();
});

$this->commands([
'xinax::gettext.create',
'xinax::gettext.update',
Commands\GettextCreate::class,
Commands\GettextUpdate::class,
]);
}

/**
* Get the services
* Get the services provided by the provider.
*
* @return array
* @return array<int, class-string|string>
*/
public function provides()
public function provides(): array
{
return [
'laravel-gettext'
'laravel-gettext',
LaravelGettext::class,
];
}
}
90 changes: 44 additions & 46 deletions src/Xinax/LaravelGettext/Support/helpers.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php

declare(strict_types=1);

use Xinax\LaravelGettext\LaravelGettext;

if (!function_exists('_i')) {
/**
* Translate a formatted string based on printf formats
* Can be use an array on args or use the number of the arguments
*
* @param string $message the message to translate
* @param array|mixed $args the tokens values used inside the $message
*
* @return string the message translated and formatted
* @param string $message The message to translate
* @param array|string|int|float|bool|null $args The tokens values used inside the $message
* @return string The message translated and formatted
*/
function _i($message, $args = null)
function _i(string $message, mixed $args = null): string
{

$translator = app(LaravelGettext::class);
/** @var LaravelGettext $translator */
$translator = app(LaravelGettext::class);
$translation = $translator->translate($message);

if (strlen($translation)) {
if (isset($args)) {
if ($translation !== '') {
if ($args !== null) {
if (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
Expand All @@ -29,12 +30,6 @@ function _i($message, $args = null)
return $translation;
}

/**
* If translations are missing returns
* the original message.
*
* @see https://github.com/symfony/symfony/issues/13483
*/
return $message;
}
}
Expand All @@ -44,12 +39,11 @@ function _i($message, $args = null)
* Translate a formatted string based on printf formats
* Can be use an array on args or use the number of the arguments
*
* @param string $message the message to translate
* @param array|mixed $args the tokens values used inside the $message
*
* @return string the message translated and formatted
* @param string $message The message to translate
* @param array|string|int|float|bool|null $args The tokens values used inside the $message
* @return string The message translated and formatted
*/
function __($message, $args = null)
function __(string $message, mixed $args = null): string
{
return _i($message, $args);
}
Expand All @@ -59,11 +53,11 @@ function __($message, $args = null)
/**
* Generic translation function
*
* @param $message
*
* @return mixed
* @param string $message The message to translate
* @param array|string|int|float|bool|null $args The tokens values used inside the $message
* @return string The message translated and formatted
*/
function _($message, $args = null)
function _(string $message, mixed $args = null): string
{
return _i($message, $args);
}
Expand All @@ -74,22 +68,24 @@ function _($message, $args = null)
* Translate a formatted pluralized string based on printf formats
* Can be use an array on args or use the number of the arguments
*
* @param string $singular the singular message to be translated
* @param string $plural the plural message to be translated if the $count > 1
* @param int $count the number of occurrence to be used to pluralize the $singular
* @param array|mixed $args the tokens values used inside $singular or $plural
*
* @return string the message translated, pluralized and formatted
* @param string $singular The singular message to be translated
* @param string $plural The plural message to be translated if the $count > 1
* @param int $count The number of occurrence to be used to pluralize the $singular
* @param array|string|int|float|bool|null $args The tokens values used inside $singular or $plural
* @return string The message translated, pluralized and formatted
*/
function _n($singular, $plural, $count, $args = null)
function _n(string $singular, string $plural, int $count, mixed $args = null): string
{
/** @var LaravelGettext $translator */
$translator = app(LaravelGettext::class);
$message = $translator->translatePlural($singular, $plural, $count);
$message = $translator->translatePlural($singular, $plural, $count);

if (isset($args) && !is_array($args)) {
$args = array_slice(func_get_args(), 3);
if ($args !== null) {
if (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
return vsprintf($message, $args);
}
$message = vsprintf($message, $args);

return $message;
}
Expand All @@ -102,22 +98,24 @@ function _n($singular, $plural, $count, $args = null)
*
* <b>Only works if Symfony is the used backend</b>
*
* @param string $message The one line message containing the different pluralization separated by pipes
* See Symfony translation documentation
* @param int $count the number of occurrence to be used to pluralize the $singular
* @param array|mixed $args the tokens values used inside $singular or $plural
*
* @return string the message translated, pluralized and formatted
* @param string $message The one line message containing the different pluralization separated by pipes
* See Symfony translation documentation
* @param int $count The number of occurrence to be used to pluralize the $singular
* @param array|string|int|float|bool|null $args The tokens values used inside $singular or $plural
* @return string The message translated, pluralized and formatted
*/
function _s($message, $count, $args = null)
function _s(string $message, int $count, mixed $args = null): string
{
/** @var LaravelGettext $translator */
$translator = app(LaravelGettext::class);
$message = $translator->getTranslator()->translatePluralInline($message, $count);
$message = $translator->getTranslator()->translatePluralInline($message, $count);

if (isset($args) && !is_array($args)) {
$args = array_slice(func_get_args(), 3);
if ($args !== null) {
if (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
return vsprintf($message, $args);
}
$message = vsprintf($message, $args);

return $message;
}
Expand Down
9 changes: 2 additions & 7 deletions tests/unit/MultipleDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,11 @@ class MultipleDomainTest extends BaseTestCase
*/
protected $storagePath;

public function __construct()
{
parent::__construct();

$this->clearFiles();
}

protected function setUp(): void
{
parent::setUp();

$this->clearFiles();

// $testConfig array
$testConfig = include __DIR__ . '/../config/config.php';
Expand Down