{"id":15293924,"url":"https://github.com/jersyfi/laravel-localization","last_synced_at":"2026-01-31T00:33:40.195Z","repository":{"id":56998196,"uuid":"339332284","full_name":"Jersyfi/laravel-localization","owner":"Jersyfi","description":"Minimalistic package to localize your laravel application in a few steps","archived":false,"fork":false,"pushed_at":"2021-11-19T07:29:13.000Z","size":163,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"v2","last_synced_at":"2025-05-29T06:04:48.038Z","etag":null,"topics":["laravel","laravel-framework","laravel-localization","laravel-package","localization","localize","multilanguage","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jersyfi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-16T08:33:41.000Z","updated_at":"2021-11-19T07:27:33.000Z","dependencies_parsed_at":"2022-08-21T11:10:20.767Z","dependency_job_id":null,"html_url":"https://github.com/Jersyfi/laravel-localization","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jersyfi%2Flaravel-localization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jersyfi%2Flaravel-localization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jersyfi%2Flaravel-localization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jersyfi%2Flaravel-localization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jersyfi","download_url":"https://codeload.github.com/Jersyfi/laravel-localization/tar.gz/refs/heads/v2","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jersyfi%2Flaravel-localization/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259039317,"owners_count":22796800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","laravel-framework","laravel-localization","laravel-package","localization","localize","multilanguage","php"],"created_at":"2024-09-30T16:53:44.516Z","updated_at":"2026-01-31T00:33:40.157Z","avatar_url":"https://github.com/Jersyfi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A package to make your application localized\r\n\r\n![Packagist Downloads](https://img.shields.io/packagist/dt/jersyfi/laravel-localization)\r\n![Packagist Version](https://img.shields.io/packagist/v/jersyfi/laravel-localization)\r\n![GitHub License](https://img.shields.io/github/license/jersyfi/laravel-localization)\r\n\r\n\r\n## Installation\r\n\r\nYou can install the package via composer\r\n\r\n```bash\r\ncomposer require jersyfi/laravel-localization\r\n```\r\n\r\nYou need to publish the config file to customize the package\r\n\r\n```bash\r\nphp artisan vendor:publish --provider=\"Jersyfi\\Localization\\LocalizationServiceProvider\" --tag=\"config\"\r\n```\r\n\r\nYou have the option to publish a migration file for adding a customizable column to a users table for storing the current locale for a logged in user. This is usefull if your application wants to send emails to every user.\r\n\r\n```bash\r\nphp artisan vendor:publish --provider=\"Jersyfi\\Localization\\LocalizationServiceProvider\" --tag=\"migrations\"\r\n```\r\n\r\nThe published config `localization` looks like so\r\n\r\n```php\r\nreturn [\r\n\r\n    /**\r\n     * Applications default locale need to be set because the config('app.locale')\r\n     * gives back the current locale and not the value from config\r\n     */\r\n    'default_locale' =\u003e 'de',\r\n\r\n    /**\r\n     * Application locales determines all locals that exists in the application\r\n     * You can ignore or delete the locales from app.locales if you set some\r\n     */\r\n    'locales' =\u003e [\r\n        'en',\r\n        'de'\r\n    ],\r\n    \r\n    /**\r\n     * Redirect to default locale when not found\r\n     */\r\n    'redirect_default' =\u003e false,\r\n\r\n    /**\r\n     * Detect user locale via http header\r\n     * When no locale is stored in session user gets redirected\r\n     */\r\n    'detect_locale' =\u003e false,\r\n    \r\n    /**\r\n     * Application can store the prefered_locale in the users database table\r\n     */\r\n    'store_users_database' =\u003e true,\r\n    \r\n    /**\r\n     * Setup for the users database table\r\n     * Only if 'store_users_database' is set to true\r\n     */\r\n    'database' =\u003e [\r\n        'users_table_name' =\u003e 'users',\r\n        'prefered_locale_column_name' =\u003e 'prefered_locale',\r\n    ],\r\n\r\n];\r\n```\r\n\r\n\r\n## How to use\r\n\r\nMore information can be found in the original Laravel documentation with version 8.x.\r\nThere you need to know everything about [Routing](https://laravel.com/docs/8.x/routing) and [Localization](https://laravel.com/docs/8.x/localization). When you also want to have translatable models i prefere to use [laravel-translatable](https://github.com/spatie/laravel-translatable) from Spatie.\r\n\r\n### Routing\r\n\r\nThe middleware is using `redirect_default` to redirect any request when the requested locale was not in `locales`. To detect the browser language when entering the page for the first time the `LocaleController` is using `detect_locale`.\r\n\r\nYou can redirect to the `default_locale` by accessing the `LocaleController` function called `localize` with the example:\r\n```php\r\nuse Jersyfi\\Localization\\Http\\Controllers\\LocaleController;\r\n\r\nRoute::get('/', [LocaleController::class, 'localize'])\r\n    -\u003ename('locale');\r\n```\r\nTo group a route it is the easiest way to set a prefix named `{locale}` together with the middleware `locale`.\r\nInside this group you can set your own localized routes.\r\nAn example to get this localized route group:\r\n```php\r\nRoute::prefix('{locale}')\r\n    -\u003emiddleware('locale')\r\n    -\u003egroup(function () {\r\n\r\n        // your localized routes here\r\n    });\r\n```\r\n\r\n### Helpers\r\n\r\nThe helpers can be accesed directly by aliases or by facades. When using aliases in your controller you need to include `use Localization`.\r\nIn below examples the used values are from the above config file so that you can compare it.\r\n\r\nReturn the given locale or the current app locale with replaced separator.\r\n```php\r\n$slug = Localization::getLocaleSlug('en_GB'); // en-gb\r\n$slug = Localization::getLocaleSlug(); // de\r\n```\r\n\r\nReturn all available locales set in the localization config.\r\n```php\r\n$locales = Localization::getLocales(); // ['en', 'de']\r\n```\r\n\r\nReturn application default locale set in the localization config.\r\n```php\r\n$locale = Localization::getDefaultLocale(); // de\r\n```\r\n\r\nReturn all available locales from localization config without the default locale.\r\n```php\r\n$locales = Localization::getLocalesWithoutDefault(); // ['en']\r\n```\r\n\r\nReturn all available locales from localization config without the current locale.\r\n```php\r\n$locales = Localization::getLocalesWithoutCurrent(); // ['de']\r\n```\r\n\r\nReturn the current Route URL\r\n```php\r\n$url = Localization::currentRouteURL(); // https://test.de/de/home?query=true\r\n```\r\n\r\nReturn the current Route URL with any different locale set in the localization config.\r\nThe function replaces the {locale} set in routes with the given locale.\r\n```php\r\n$url = Localization::currentRouteLocaleURL('en'); // https://test.de/en/home?query=true\r\n```\r\n\r\nReturn the current Route URL with default locale set in the localization config.\r\n```php\r\n$url = Localization::currentRouteDefaultLocaleURL(); // https://test.de/de/home?query=true\r\n```\r\n\r\nCheck if the locales are valid\r\n```php\r\n$valid = Localization::localeIsValid('de'); // true\r\n$valid = Localization::localeIsValid('de', 'en'); // true\r\n$valid = Localization::localeIsValid('de', 'sp'); // false\r\n```\r\n\r\n\r\n## Examples\r\n\r\n### Route to named routes\r\n\r\nWe create a simple index Route named `home` calling whatever you want. In this example we call a Controller. Then you can call your route from whereever you want with `route('home')`.\r\n```php\r\nRoute::get('/', [LocaleController::class, 'localize'])\r\n    -\u003ename('locale');\r\n\r\nRoute::prefix('{locale}')\r\n    -\u003emiddleware('locale')\r\n    -\u003egroup(function () {\r\n\r\n        // your localized routes here\r\n        Route::get('/', [HomeController::class, 'index'])\r\n            -\u003ename('home');\r\n    });\r\n```\r\n```html\r\n\u003ca href=\"{{ route('home') }}\"\u003eHome\u003c/a\u003e\r\n```\r\n\r\n### Create a canonical link\r\n\r\nYou need to call the helper function `Localization::currentRouteDefaultLocaleURL()`\r\n```html\r\n\u003clink rel=\"canonical\" href=\"{{ Localization::currentRouteDefaultLocaleURL() }}\"\u003e\r\n```\r\n\r\n### Create alternate links\r\n\r\nTo get all alternate links without the default locale you can call the helper function `Localization::getLocalesWithoutDefault()` inside a foreach loop. Inside the href of the html you can call the helper function `Localization::currentRouteLocaleURL()` and pass the `$locale` to it.\r\n```html\r\n@foreach(Localization::getLocalesWithoutDefault() as $locale)\r\n    \u003clink rel=\"alternate\" hreflang=\"{{ Localization::getLocaleSlug($locale) }}\" href=\"{{ Localization::currentRouteLocaleURL($locale) }}\"\u003e\r\n@endforeach\r\n```\r\n\r\n### Create a language selector\r\n\r\nWhen you want to create a language selector first you need the current locale slug. For this you can call the helper function `Localization::getLocaleSlug()`. To loop the other locales you can decide if you want to display all available locales with `Localization::getLocales()` or if you want to display the available locales without the current locale with `Localization::getLocalesWithoutCurrent()`. Inside the foreach loop you can call the helper function `Localization::currentRouteLocaleURL($locale)` to get the link and `Localization::getLocalSlug($locale)` for the locale slug.\r\n```html\r\n\u003cdiv\u003e{{ Localization::getLocaleSlug() }}\u003c/div\u003e\r\n\u003cul\u003e\r\n    @foreach(Localization::getLocalesWithoutCurrent() as $locale)\r\n        \u003cli\u003e\r\n            \u003ca href=\"{{ Localization::currentRouteLocaleURL($locale) }}\"\u003e{{ Localization::getLocalSlug($locale) }}\u003c/a\u003e\r\n        \u003c/li\u003e\r\n    @endforeach\r\n\u003c/ul\u003e\r\n```\r\n\r\n\r\n## Changelog\r\n\r\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\r\n\r\n## Credits\r\n\r\n- [Jérôme Bastian Winkel](https://github.com/jersyfi)\r\n- [All Contributors](../../contributors)\r\n\r\n## License\r\n\r\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjersyfi%2Flaravel-localization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjersyfi%2Flaravel-localization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjersyfi%2Flaravel-localization/lists"}