This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
# bitrix-translation | ||
[![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, | ||
], | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "ge1i0n/bitrix-translation", | ||
"license": "MIT", | ||
"keywords": [ | ||
"bitrix", | ||
"translation", | ||
"localization" | ||
], | ||
"authors": [{ | ||
"name": "Gennadiy Sevalnev", | ||
"email": "[email protected]" | ||
}], | ||
"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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Gelion\BitrixTranslation; | ||
|
||
use Illuminate\Container\Container; | ||
|
||
class TranslationProvider | ||
{ | ||
public static function register() | ||
{ | ||
$provider = new TranslationServiceProvider(Container::getInstance()); | ||
$provider->register(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Gelion\BitrixTranslation; | ||
|
||
use Bitrix\Main\Config\Configuration; | ||
use Illuminate\Translation\FileLoader; | ||
use Illuminate\Translation\TranslationServiceProvider as LaravelTranslationServiceProvider; | ||
use Illuminate\Translation\Translator; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class TranslationServiceProvider extends LaravelTranslationServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
use Illuminate\Container\Container; | ||
|
||
if (!function_exists('app')) { | ||
/** | ||
* Get the available container instance. | ||
* | ||
* @param string|null $abstract | ||
* @param array $parameters | ||
* @return mixed|\Illuminate\Contracts\Foundation\Application | ||
*/ | ||
function app($abstract = null, array $parameters = []) | ||
{ | ||
if (is_null($abstract)) { | ||
return Container::getInstance(); | ||
} | ||
|
||
return Container::getInstance()->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); | ||
} | ||
} |