Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Add base package files
Browse files Browse the repository at this point in the history
  • Loading branch information
Ge1i0N committed Oct 22, 2021
1 parent 1986dc8 commit 5bfc685
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
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,
],
```
28 changes: 28 additions & 0 deletions composer.json
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"
]
}
}
14 changes: 14 additions & 0 deletions src/TranslationProvider.php
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();
}
}
65 changes: 65 additions & 0 deletions src/TranslationServiceProvider.php
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'];
}
}
75 changes: 75 additions & 0 deletions src/functions.php
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);
}
}

0 comments on commit 5bfc685

Please sign in to comment.