From 5bfc68521ab41292f7b12abc5dfff5eb714740ec Mon Sep 17 00:00:00 2001 From: Gennadiy Sevalnev Date: Fri, 22 Oct 2021 14:28:06 +0700 Subject: [PATCH] Add base package files --- README.md | 42 ++++++++++++++++- composer.json | 28 +++++++++++ src/TranslationProvider.php | 14 ++++++ src/TranslationServiceProvider.php | 65 ++++++++++++++++++++++++++ src/functions.php | 75 ++++++++++++++++++++++++++++++ 5 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 composer.json create mode 100644 src/TranslationProvider.php create mode 100755 src/TranslationServiceProvider.php create mode 100755 src/functions.php diff --git a/README.md b/README.md index 64887d5..463353a 100644 --- a/README.md +++ b/README.md @@ -1 +1,41 @@ -# bitrix-translation \ No newline at end of file +[![Latest Stable Version](https://poser.pugx.org/ge1i0n/bitrix-translation/v/stable.svg)](https://packagist.org/packages/ge1i0n/bitrix-translation/) +[![Total Downloads](https://img.shields.io/packagist/dt/ge1i0n/bitrix-translation.svg?style=flat)](https://packagist.org/packages/ge1i0n/bitrix-translation) + +# Bitrix Translation - интеграция функционала локализации фреймворка Laravel в Битрикс + +## Установка + +1. ```composer require ge1i0n/bitrix-translation``` + +2. Добавляем в init.php + +```php + +use Gelion\BitrixTranslation\TranslationProvider; + +require $_SERVER['DOCUMENT_ROOT']."/vendor/autoload.php"; + +TranslationProvider::register(); +``` + +## Использование + +Вызов строк локализации [аналогичен Laravel](https://laravel.com/docs/8.x/localization#defining-translation-strings). +Для смены текущего языка используйте конструкцию: +```php +app('translator')->setLocale('en'); +``` +## Конфигурация + +При необходимости пути можно поменять в конфигурации. +.settings_extra.php +```php +'bitrix-translation' => [ + 'value' => [ + 'langPath' => '/absolute/path/or/path/from/document/root', // по умолчанию 'local/lang' + 'locale' => 'ru', // по умолчанию 'ru' + 'fallback_locale' => 'ru', // по умолчанию 'ru' + ], + 'readonly' => true, +], +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8474b7d --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "ge1i0n/bitrix-translation", + "license": "MIT", + "keywords": [ + "bitrix", + "translation", + "localization" + ], + "authors": [{ + "name": "Gennadiy Sevalnev", + "email": "gennadiy.sevalnev@mail.ru" + }], + "homepage": "https://github.com/ge1i0n/bitrix-translation", + "require": { + "php": ">=7.3.0", + "illuminate/config": "8.*", + "illuminate/container": "8.*", + "illuminate/translation": "8.*" + }, + "autoload": { + "psr-4": { + "Gelion\\BitrixTranslation\\": "src/" + }, + "files": [ + "src/functions.php" + ] + } +} \ No newline at end of file diff --git a/src/TranslationProvider.php b/src/TranslationProvider.php new file mode 100644 index 0000000..07a7fde --- /dev/null +++ b/src/TranslationProvider.php @@ -0,0 +1,14 @@ +register(); + } +} diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php new file mode 100755 index 0000000..1bbdc19 --- /dev/null +++ b/src/TranslationServiceProvider.php @@ -0,0 +1,65 @@ +registerLoader(); + $bitrixConfig = Configuration::getValue('bitrix-translation'); + + $locale = isset($bitrixConfig['locale']) ? $bitrixConfig['locale'] : 'ru'; + $fallback_locale = isset($bitrixConfig['fallback_locale']) ? $bitrixConfig['fallback_locale'] : 'ru'; + + $this->app->singleton('translator', function ($app) use ($locale, $fallback_locale) { + $loader = $app['translation.loader']; + + $trans = new Translator($loader, $locale); + + $trans->setFallback($fallback_locale); + + return $trans; + }); + + $this->app->singleton('files', function () { + return new Filesystem(); + }); + } + + /** + * Register the translation line loader. + * + * @return void + */ + protected function registerLoader() + { + $bitrixConfig = Configuration::getValue('bitrix-translation'); + $langPath = isset($bitrixConfig['langPath']) ? $bitrixConfig['langPath'] : 'local/lang'; + + $this->app->singleton('translation.loader', function ($app) use ($langPath) { + return new FileLoader($app['files'], $langPath); + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['translator', 'translation.loader']; + } +} diff --git a/src/functions.php b/src/functions.php new file mode 100755 index 0000000..26341d5 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,75 @@ +make($abstract, $parameters); + } +} + +if (!function_exists('trans')) { + /** + * Translate the given message. + * + * @param string|null $key + * @param array $replace + * @param string|null $locale + * @return \Illuminate\Contracts\Translation\Translator|string|array|null + */ + function trans($key = null, $replace = [], $locale = null) + { + if (is_null($key)) { + return app('translator'); + } + + return app('translator')->get($key, $replace, $locale); + } +} + +if (!function_exists('trans_choice')) { + /** + * Translates the given message based on a count. + * + * @param string $key + * @param \Countable|int|array $number + * @param array $replace + * @param string|null $locale + * @return string + */ + function trans_choice($key, $number, array $replace = [], $locale = null) + { + return app('translator')->choice($key, $number, $replace, $locale); + } +} + +if (!function_exists('__')) { + /** + * Translate the given message. + * + * @param string|null $key + * @param array $replace + * @param string|null $locale + * @return string|array|null + */ + function __($key = null, $replace = [], $locale = null) + { + if (is_null($key)) { + return $key; + } + + return trans($key, $replace, $locale); + } +}