Easy Localization for Laravel

Overview

laravel-package_2

Query String Localization Package for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads


Use this package to localize your laravel application via query strings easily.

Features:

  • Localization based on a query string.
  • Includes a Livewire language selector component.
  • Optionally stores the user language preference to the user's table and apply it for every session.

Installation

To get started, require the package via composer:

composer require cosnavel/laravel-query-localization

Config File

You can publish the config file with:

php artisan vendor:publish --provider="Cosnavel\LaravelQueryLocalization\LaravelQueryLocalizationServiceProvider" --tag="laravel-query-localization-config"

After publishing, config query-localization.php will be created.

The configuration options are:

  • supportedLocales Languages of your app (Default: English & German).

  • useAcceptLanguageHeader If true, then automatically detect language from browser.

  • useUserLanguagePreference If true, save the language preference of an authenticated user in the database and apply the preference on each session

Usage

Register Middleware

To get started, register the LocaleFromQuery middleware for the Route Group that needs to be localized.

// routes/web.php


Route::middleware(LocaleFromQuery::class)->group(function () {
    Route::view('/', 'welcome');
});

User Language Preference

If you want to save the language preference to the users table:

  • publish the config file
  • enable useLanguageUserPreference in the config file
  • publish and run the migrations

You can publish and run the migrations with:

php artisan vendor:publish --provider="Cosnavel\LaravelQueryLocalization\LaravelQueryLocalizationServiceProvider" --tag="query-localization-migrations"
php artisan migrate
  • if you want to use mass assignment for the language_preference field in the users table make the field fillable in the user model

Helpers

Get Supported Locales

Return all supported locales and their properties as an array.

Cosnavel\LaravelQueryLocalization\Facades\LaravelQueryLocalization::getSupportedLocales();

Get Current Locale

Return the key of the current locale.

  • Return the current locale from the session
  • if none found, it negotiates locale from acceptLanguageHeaders.
  • when acceptLanguageHeaders option is disabled, and no value is available in the session, use the applications default locale
Cosnavel\LaravelQueryLocalization\Facades\LaravelQueryLocalization::getCurrentLocale();

Determine Valid Locales

Checks if the passed locale is a supportedLocale (check the config to add your needed locales). If it is not, the returned locale is the application's default locale.

Cosnavel\LaravelQueryLocalization\Facades\LaravelQueryLocalization::determineValidLanguage('en');

Set Locale

Set locale programmatically. Internally the passed locale gets determined if it's a valid locale.

Cosnavel\LaravelQueryLocalization\Facades\LaravelQueryLocalization::setLocale('en');

Set User Language Preference

Set an authed user's language preference. The passed language will also be checked for availability.

Cosnavel\LaravelQueryLocalization\Facades\LaravelQueryLocalization::setUserLanguagePreference('en');

Language Selector

If you're supporting multiple locales in your project, you will probably want to provide the users with a way to change the language.

Included in this package is a language selector. The Language Selector is built with Tailwind, Alpine & Livewire.

CleanShot.2021-08-06.at.11.32.42.mp4

Alpine

The Language Picker requires Alpine. You can use the official CDN to quickly include Alpine:

<!-- Alpine v2 -->
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.min.js" defer></script>

<!-- Alpine v3 -->
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

Usage

Just include the Livewire Component in your blade view. All available locales from your config will be used.

@livewire('language-selector')

When useUserLanguagePreference is enabled, the language preference of an authenticated user will be set.

If you don't want to use Tailwind or want to customize the language picker, I recommend that you publish the component the markup as you like.

php artisan vendor:publish --provider="Cosnavel\LaravelQueryLocalization\LaravelQueryLocalizationServiceProvider" --tag="query-localization-views"

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Easy-to-install Admin Panel for Laravel

CSS Framework: https://0notole.github.io/elements.css/ Install project: composer create-project --prefer-dist laravel/laravel screen, cd screen Add re

laravel-wallet - Easy work with virtual wallet.
laravel-wallet - Easy work with virtual wallet.

laravel-wallet - Easy work with virtual wallet. [Documentation] [Get Started] [Документация] [Как начать] Vendor: bavix Package: laravel-wallet Versio

A premade, easy to use local development setup to be used for authoring Laravel applications

Laravel Drydock This project is a premade, easy to use local development setup to be used for authoring Laravel applications. The deliverables of this

An easy-to-use virtual wallet implementation for Laravel

Laravel Wallet Some apps require a prepayment system like a virtual wallet where customers can recharge credits which they can then use to pay in app

An easy way to integrate Google Maps with Laravel

An easy way to integrate Google Maps with Laravel For Laravel 5.x, check version 2.35.1 For Laravel 4.x, check version 1.27.0 Think of Googlmapper as

Feature Switching made easy in Laravel 5

Feature Switching (made easy) for Laravel Need to wrap new features for dev and production? Use a directive in the view or alias in the controller The

Simple Arabic Laravel Dashboard , has basic settings and a nice layout . to make it easy for you to create fast dashboard
Simple Arabic Laravel Dashboard , has basic settings and a nice layout . to make it easy for you to create fast dashboard

Simple Arabic Laravel Dashboard ✅ Auto Seo ✅ Optimized Notifications With Images ✅ Smart Alerts ✅ Auto Js Validations ✅ Front End Alert ✅ Nice Image V

Builds nice, normalized and easy to consume REST JSON responses for Laravel powered APIs.

REST API Response Builder for Laravel Master branch: Development branch: Table of contents Introduction Why should I use it? Usage examples Features E

Easy Laravel Server-Side implementation of PrimeVue Datatables

Laravel + PrimeVue Datatables This is a simple, clean and fluent serve-side implementation of PrimeVue Datatables in Laravel. Features Global Search i

Releases(0.3.0)
Owner
Cosnavel
Software Dev and loving it ʕʘ̅͜ʘ̅ʔ
Cosnavel
A Laravel Larex plugin to import/export localization strings from/to Crowdin

Laravel Larex: Crowdin Plugin A Laravel Larex plugin to import/export localization strings from/to Crowdin ?? Requirements PHP 7.4 | 8.0 Laravel ≥ 7 L

Luca Patera 4 Oct 27, 2021
Seo Manager Package for Laravel ( with Localization )

Seo Manager Package for Laravel ( with Localization ) lionix/seo-manager package will provide you an interface from where you can manage all your page

Lionix 205 Dec 23, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
Postgis extensions for laravel. Aims to make it easy to work with geometries from laravel models.

Laravel Wrapper for PostgreSQL's Geo-Extension Postgis Features Work with geometry classes instead of arrays. $model->myPoint = new Point(1,2); //lat

Max 340 Jan 6, 2023
Laravel Breadcrumbs - An easy way to add breadcrumbs to your @Laravel app.

Introduction Breadcrumbs display a list of links indicating the position of the current page in the whole site hierarchy. For example, breadcrumbs lik

Alexandr Chernyaev 269 Dec 21, 2022
Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud Messaging (FCM).

Laravel-FCM Introduction Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud M

Rahul Thapa 2 Oct 16, 2022
Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.

Nebula Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS. Nebula m

Nebula 228 Nov 11, 2022
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.6k Dec 30, 2022
Laravel Serializable Closure provides an easy way to serialize closures in PHP.

Serializable Closure Introduction This package is a work in progress Laravel Serializable Closure provides an easy way to serialize closures in PHP. I

The Laravel Framework 316 Jan 1, 2023
Larawiz is a easy project scaffolder for Laravel

Larawiz The Laravel 8 scaffolder you wanted but never got, until now! Use a single YAML file to create models, migrations, factories, seeders, pivot t

Larawiz 139 Aug 19, 2022