简化版语言包,适用于Laravel

1.2.02 2018-06-26 15:19 UTC

This package is auto-updated.

Last update: 2024-09-20 05:26:17 UTC


README

一个简单的翻译工具,用于转换UI和本地语言。由PhpAnonymous ( phpanonymous.com ) 创建和开发,支持Laravel 5及以上版本

## 使用Composer安装

composer require Langnonymous/Lang 

提供者类

此提供者将自动注册

  Langnonymous\Lang\Langnonymous::class,
 

# 别名 这些别名将自动注册

'L'         => Langnonymous\Lang\Lang::class,

# 使用Composer发布 运行以下命令 php artisan vendor:publish

现在您可以在配置路径下查看此文件 langnonymous.php

您应该在 kernel.php 文件中添加此中间件

'Lang'     => \Langnonymous\Lang\Lang::class,

// like This 
  protected $routeMiddleware = [        
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'Lang'     => \Langnonymous\Lang\Lang::class,
    ];

# 用法

您可以在任何地方使用 L 类,例如控制器或Blade文件

现在您应该在 web.php 或任何路由文件中添加此方法来触发我们的操作

L::LangNonymous();

如果您有管理员面板并且想要访问语言路由路径,请将此添加到路由文件中,并设置路径如 admin !!

L::Panel('admin');

现在您已经准备好看到这样的路由。最好将这些行放在文件的第一行

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

L::Panel('admin');
L::LangNonymous();

好的,您必须添加此公开中间件,并将所有路由放入此中间件中

Route::group(['middleware'=>'Lang'],function(){
// put all routes here please ...i'ts fine :)
});

现在您可能想要配置 langnonymous 文件

<?php 

return [
		'UserModeLang'=>true, // true,false | if you want save lang in User Tbl Set true auto detected user lang
		'LangRoute'=>'langnonymous', // Route Name You Can Change Route Name
		'column_lang'=>'lang', // You May put The Defualt column if you are enable UserModeLang for true
		'languages'=>['ar','en','es','jp'], // Put Your Language website Usage
		'defaultLanguage'=>'ar', // Set Your Default Language (ar,en,es Any Short Lang From languages array)
		'redirectAfterSet'=>'back', //Set Direction home,back | Back reflect to function back | home to index or other route
];

这是包中内置的方法和类,您可以在任何地方使用它们

app('lang');
app('l');
// if you want methods !! okay that's was easy
L::lang();
L::l();
// master session 
session('anonylang');
// you have a style directions !! don't worry
// use this singletone in your file css or js :) whatever like example.com/css/style-rtl.css :) or rtl
app('dir'); // RTL OR LTR
L::dir(); // RTL or LTR
//example.com/css/style-{{app('dir')}}.css from link tag
//example.com/js/jquery-{{app('dir')}}.css from script tag 

// attention 
//if you are enable UserModeLang .. you should add column name to column_lang from user table in sql 
// you maybe make a new folders to usage this array 'languages'=>['ar','en','es','jp']
/*
resource/lang/ar
set file name and put this array
<?php 
return [
		'ar'=>'العربية',
		'en'=>'English',
		'es'=>'Spanish',
		'jp'=>'日本の',
		'welcome'=>'مرحبا',
	];
   demo trans('yourfile.welcome') // مرحبا
  
resource/lang/en
set file name and put this array
<?php 
return [
		'ar'=>'العربية',
		'en'=>'English',
		'es'=>'Spanish',
		'jp'=>'日本の',
		'welcome'=>'Welcome',
	];
   demo trans('yourfile.welcome') // welcome
resource/lang/es
set file name and put this array
<?php 
return [
		'ar'=>'العربية',
		'en'=>'English',
		'es'=>'Spanish',
		'jp'=>'日本の',
		'welcome'=>'bienvenida',
	];
   demo trans('yourfile.welcome') // bienvenida
resource/lang/jp
set file name and put this array
<?php 
return [
		'ar'=>'العربية',
		'en'=>'English',
		'es'=>'Spanish',
		'jp'=>'日本の',
		'welcome'=>'もしもし',
	];
  demo trans('yourfile.welcome') // もしもし
or you can custom any language needed want
*/

// to set lang on your web okay follow this 
// in blade file put master language 
<a href="{{L::put('ar')}}">{{trans('yourfile.ar')}}</a>
<a href="{{L::put('en')}}">{{trans('yourfile.en')}}</a>
<a href="{{L::put('es')}}">{{trans('yourfile.es')}}</a>
<a href="{{L::put('jp')}}">{{trans('yourfile.jp')}}</a>
// or you can loop all automatically with method L::all();
@foreach(L::all() as $lang)
                            <a href="{{L::put($lang)}}">{{trans('yourfile.'.$lang)}}</a> . 
                 @endforeach
// for singleton 

如果您对此包有任何疑问,请加入我们的 Facebook 群组 (https://#/groups/anonymouses.developers)

享受吧 :)