Giter Club home page Giter Club logo

laravel-multilang's Introduction

Laravel 5.x MultiLanguage

Build Status Code Coverage Code Quality Latest Stable Version Total Downloads Downloads Month License

This version of MultiLang package requires minimum PHP 7.0. For older PHP versions use MultiLang 1.x

This is a very useful package to integrate multi language (multi locale) functionality in Laravel 5.x. It includes a ServiceProvider to register the multilang and Middleware for automatic modification routes like http://site.com/en/your-routes.

This package uses database for storing translations (it caches data on production environment for improving performance) Also package automatically adds in database missing keys (on the local environment only).

Table of Contents

Installation

Install this package through Composer.

Edit your project's composer.json file to require longman/laravel-multilang

Create composer.json file:

{
    "name": "yourproject/yourproject",
    "type": "project",
    "require": {
        "longman/laravel-multilang": "~2.0"
    }
}

And run composer update

Or run a command in your command line:

composer require longman/laravel-multilang

In Laravel the service provider and facade will automatically get registered.

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider="Longman\LaravelMultiLang\MultiLangServiceProvider"

After run multilang migration command

php artisan multilang:migration

Its creates multilang migration file in your database/migrations folder. After you can run

php artisan migrate

Also if you want automatically change locale depending on url (like http://site.com/en/your-routes) you must add middleware in app/Http/Kernel.php

I suggest add multilang after CheckForMaintenanceMode middleware

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Longman\LaravelMultiLang\Middleware\MultiLang::class,
];

In your RoutesServiceProvider modify that:

MultiLang::routeGroup(function($router) {
    require app_path('Http/routes.php');
});

or directly in app/Http/routes.php file add multilang group:

MultiLang::routeGroup(function($router) {
    // your routes and route groups here
});

Or if you want only translate strings without automatic resolving locale and redirect, you can manually set locale in your application like:

App::setLocale('en');

Usage

Translating

Everywhere in application you can use t() helper function like:

$string = t('Your translatable string');

You can use markers for dynamic texts and pass any data like

$string = t('The :attribute must be a date after :date.', ['attribute' => 'Start Date', 'date' => '7 April 1986']);

which will be return The Start Date must be a date after 7 April 1986.

Blade Templates

In blade templates you can use just @t() notation like

@t('Your translatable string')

which is equivalent to {{ t('Your translatable string') }}

URL Generation

Also you can use lang_url() helper function for appending current lang marker in urls automatically.

$url = lang_url('users'); // which returns /en/users depending on your language (locale)

You can force locale and get localized url for current url for example.

$url = lang_url('users', [], null, 'ka'); // which returns /ka/users ignoring current locale

or

$url = lang_url('en/users', [], null, 'ka'); // also returns /ka/users

Also you use named routes via lang_route() function

$url = lang_route('users'); // which returns en.users depending on your language (locale)

Also Request::locale() always will return current locale.

Note: Texts will be selected after firing Laravel's LocaleUpdated event. Therefore you should use MultiLang middleware, or manually set locale in the application.

Text Scopes

If you want group translations by some scope, in package available defining of scopes. For example to define scope admin in application, you should call:

app('multilang')->setScope('admin');

before setting the locale.

Note: Default scope is global

Import/Export Texts

For versioning texts with source code (git/svn) and easy management, there is possible import texts from yml file and also export in the file.

yml file format is:

-
  key: 'authorization'
  texts:
    en: 'Authorization'
    ge: 'ავტორიზაცია'
-
  key: 'registration'
  texts:
    en: 'Registration'
    ge: 'რეგისტრაცია'

Run commands for possible options and more information:

php artisan help multilang:import

php artisan help multilang:export

TODO

write more tests

Troubleshooting

If you like living on the edge, please report any bugs you find on the laravel-multilang issues page.

Contributing

Pull requests are welcome. See CONTRIBUTING.md for information.

License

Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.

Credits

Full credit list in CREDITS

laravel-multilang's People

Contributors

akalongman avatar dependabot[bot] avatar marttosc avatar papavadze avatar skylinegtrs avatar thecotne avatar unnawut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-multilang's Issues

Route [welcome] not defined. (View: C:\xampp\htdocs\blog\resources\views\welcome.blade.php)

When i add two routes in MultiLang::routeGroup in web.php file one is home and second is welcome.

MultiLang::routeGroup(function($router) {
Route::get('home', [IndexController::class, 'home'])->name('home');
Route::get('welcome', [IndexController::class, 'welcome'])->name('welcome');
});

So in IndexController create home method and create welcome.blade.php file.
now in blade file i have defined route('welcome') so it's return this error.

Routes, Scopes, Usage Multilang t in routes

I am trying to use your multilang package for laravel, and now i am trying to find way how to use it in the routes with scope and in another place with another scope.

I want use t('something') as url for example:

Route::get(t('something')."/".t('something2'), '\Modules\Page\Http\Controllers\Front\PageController@index')->name('welcome');

i need for usage in the routes another scope (use scopes as categories for better recognizing)

i tried app('multilang')->setScope('admin'); for routes before Multilang routes

and another inside controller

app('multilang')->setScope('sitel');

$text = t('test');

but only last aplied (global)

Please can u tell me how i can create groups of texts with scope, or how to use scope properly.

Unable to exclude the admin urls from Multilang

Can you please guide me how can I exclude some of the urls that I dont want translated?

All the routes are being redirected automatically to en/route. I need to Exclude all the admin routes from multilang. I can see the exclude segment array in config , how can I use it?

Is it possible to use the `accept-languages` header to redirect to a locale instead of the config file?

On the initial redirect of the url / currently the MultiLang middleware uses the first segment as the locale or uses the fallback in the config file as the locale. Would it be possible try to use the accept-languages header first instead of going to fall back right away?

Something like (Ps: this is untested code):

$acceptLanguages = explode(';', $request->header('accept-languages');
$locale = null;
foreach($acceptLanguages as $lang) {
    if(in_array($lang, array_keys(config('multilang.locales')))) {
        $locale = $lang;
        break;
    }
}
// More code that sets the locale segment on the url and whatnot using the locale above

Between lines 271 and 272 of Multilang.php would make this a possibility I think.

Of course this could make a Denial of Service attack possible if someone passes a really long accept-languages header, so there is a need to check its length of something like that, but I belive this is better than just using the default locale on the config file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.