{"id":22138738,"url":"https://github.com/laravelista/loki","last_synced_at":"2025-07-25T22:32:08.446Z","repository":{"id":57012256,"uuid":"139908003","full_name":"laravelista/loki","owner":"laravelista","description":"Laravel localization done right.","archived":false,"fork":false,"pushed_at":"2020-07-27T06:22:03.000Z","size":42,"stargazers_count":81,"open_issues_count":7,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-31T19:42:25.884Z","etag":null,"topics":["laravel","localization","package","php"],"latest_commit_sha":null,"homepage":null,"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/laravelista.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"patreon":"laravelista"}},"created_at":"2018-07-05T22:49:18.000Z","updated_at":"2023-01-20T12:50:33.000Z","dependencies_parsed_at":"2022-08-21T15:10:48.798Z","dependency_job_id":null,"html_url":"https://github.com/laravelista/loki","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravelista%2Floki","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravelista%2Floki/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravelista%2Floki/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laravelista%2Floki/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laravelista","download_url":"https://codeload.github.com/laravelista/loki/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227469698,"owners_count":17778929,"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","localization","package","php"],"created_at":"2024-12-01T20:11:44.500Z","updated_at":"2024-12-01T20:11:45.230Z","avatar_url":"https://github.com/laravelista.png","language":"PHP","funding_links":["https://patreon.com/laravelista","https://www.patreon.com/laravelista"],"categories":[],"sub_categories":[],"readme":"# Loki\n\nLaravel localization done right.\n\n[![Become a Patron](https://img.shields.io/badge/Become%20a-Patron-f96854.svg?style=for-the-badge)](https://www.patreon.com/laravelista)\n\n## Overview\n\nIf you are building a multilingual website and you need URL management then this is the package for you. It integrates into existing Laravel functionality to support translated URLs and custom locales.\n\n**It supports route caching out of the box.**\n\nBefore, I was using [mcamara/laravel-localization\n](https://github.com/mcamara/laravel-localization) to handle this, but I really really needed route caching to work. So, I created this package and simplified the whole setup and integration process.\n\nWhatever `mcamara/laravel-localization` package can do, Loki can do too, but better.\n\n### Features\n\n- [x] Simple installation\n- [x] Easy configuration\n- [x] Custom locales\n- [x] Hide default locale\n- [x] Translated routes\n- [x] Language selector\n- [x] Route caching\n- [x] Native Laravel helper functions (`route` and `url`)\n- [x] Support for non localized routes\n- [x] Slug translation\n\n## Installation\n\nFrom the command line:\n\n```\ncomposer require laravelista/loki\n```\n\nThen, add the `Loki` trait to your `RouteServiceProvider` class:\n\n```\nuse Laravelista\\Loki\\Loki;\n\nclass RouteServiceProvider extends ServiceProvider\n{\n    use Loki;\n}\n```\n\n\u003e If you don't intend to use translated URLs (by default it is set to false) then publish the route file with the command bellow. Otherwise be sure to publish the config file, set `useTranslatedUrls` to `true` and create appropriate route files `{locale}.web.php`.\n\nPublish the localized routes file `loki.web.php` to your `/routes` directory:\n\n```\nphp artisan vendor:publish --provider=\"Laravelista\\Loki\\ServiceProvider\" --tag=route\n```\n\nNow, add `$this-\u003emapLocalizedWebRoutes();` to the `map` method in the `RouteServiceProvider`.\n\n**Installation complete!**\n\n---\n\nPut your localized routes in `/routes/loki.web.php` file. If you are using translated URLs create a route file for each locale `{locale}.web.php` (eg. `en.web.php`). Your non localized routes can remain in the `/routes/web.php` file. To access non localized routes use `URL::getNonLocalizedRoute` or `URL::getNonLocalizedUrl`. See the helpers chapter bellow to find out more.\n\n**That's it!** View the configuration chapter bellow to configure your preferences.\n\n### Configuration\n\nPublish the config file with:\n\n```\nphp artisan vendor:publish --provider=\"Laravelista\\Loki\\ServiceProvider\" --tag=config\n```\n\nYou will find it under `config/loki.php`.\n\n#### `supportedLocales` [array]\n\nLocale names (codes) can be whatever you want.\n\n_example._ en-GB, hr-HR, en-US, english, croatian, german, de, fr, ...\n\n```\n'supportedLocales' =\u003e ['hr', 'en'],\n```\n\n#### `defaultLocale` [string]\n\nThe default application locale must be from one of the locales defined in `supportedLocales`.\n\n```\n'defaultLocale' =\u003e 'en',\n```\n\n#### `hideDefaultLocale` [boolean]\n\nIf you want to hide the default locale in your URL set this to true. (The default is `true`.)\n\n_example._ If your default locale is set to `en` then requests to URLs starting with `/en` will be redirected to `/`.\n\n```\n'hideDefaultLocale' =\u003e true,\n```\n\n#### `useTranslatedUrls` [boolean]\n\nThis enables you to use localized routes. (The default is `false`.)\n\nIf you are using translated URLs for each locale then set this to `true`.\n\n_example._ `/en/about-us` on `en` locale will be `/hr/o-nama` on `hr` locale.\n\n```\n'useTranslatedUrls' =\u003e true,\n```\n\nOnce this option is set to  `true` you have to create a routes file for each locale with the prefix of the locale.\n\n`routes/en.web.php`:\n\n```\n\u003c?php\n\nRoute::get('/', 'SampleController@home')-\u003ename('home');\nRoute::get('contact', 'SampleController@contact')-\u003ename('contact');\nRoute::get('about', 'SampleController@about')-\u003ename('about');\n```\n\n`routes/hr.web.php`:\n\n```\n\u003c?php\n\nRoute::get('/', 'SampleController@home')-\u003ename('home');\nRoute::get('kontakt', 'SampleController@contact')-\u003ename('contact');\nRoute::get('o-nama', 'SampleController@about')-\u003ename('about');\n```\n\n**If you have this enabled, be sure to use route names otherwise it will not work correctly.**\n\n##### Resource routes\n\nIf you are using `Route::resource` and have set `useTranslatedUrls` to `true` you will have to set the names for the resource manually or stop using `resource` mapper and manually map resource routes.\n\n*Syntax for the `show` method, but you can apply the same to others.*\n\n`en.web.php`\n\n```\nRoute::resource('news', 'NewsController')-\u003eonly(['show']);\n```\n\n`hr.web.php`\n\n```\nRoute::resource('novosti', 'NewsController')-\u003eonly(['show'])-\u003enames([\n    'show' =\u003e 'news.show'\n]);\n```\n\n## Helpers\n\n**The default Laravel helper functions `route` and `url` have been changed to support URL localization. So you can use those as you normally would.** This enables you to easily swap the `mcamara/laravel-localization` package with this one. Also, you can install this package, configure your supported locales and your route and URL links will work without the need to change anything.\n\n\u003e The bellow helper methods are for edge cases where you want to retrieve the URL for specific locale or just get the current URL in specific locale.\n\n\n#### `URL::getNonLocalizedRoute($name, $parameters = [], $absolute = true)`\n\nIt will return the URL for the given route name to the route located in `/routes/web.php` file.\n\n**Don't use the same route names for routes in `web.php` and `loki.web.php`.**\n\n\n#### `URL::getNonLocalizedUrl($path, $extra = [], $secure = null)`\n\nIt will return the URL for the given path as is.\n\n\n#### `URL::getLocalizedRoute($locale, $name = null, $parameters = [], $absolute = true)`\n\nThere are two ways of using this method:\n\n1. Specify just the `$locale` - it will return the current route in the specified locale\n2. Specify the `$locale` and the route `$name` - it will return the URL to the given route name for given locale\n\n**If you are using translated routes be sure to use this method if needed.**\n\n\n#### `URL::getLocalizedUrl($locale, $path = null, $extra = [], $secure = null)`\n\nThere are two ways of using this method:\n\n1. Specify just the `$locale` - it will return the current URL in the specified locale\n2. Specify the `$locale` and the `$path` - it will return the URL to the given path for given locale\n\n\n#### `URL::overrideParameters($locale, array|string $parameters)`\n\nThis method will set the parameters for given locale, so that when method `getLocalizedRoute`\nis called it will use these parameters instead of the already present parameters.\n\nUseful for translating slugs. Remember to include all parameters.\n\n\n## Language switcher\n\nUse this blade template snippet to enable users to change the language:\n\n```\n\u003cul\u003e\n    @foreach(config('loki.supportedLocales') as $locale)\n        \u003cli\u003e\n            \u003ca rel=\"alternate\" hreflang=\"{{ $locale }}\" href=\"{{ URL::getLocalizedRoute($locale) }}\"\u003e\n                {{ $locale }}\n            \u003c/a\u003e\n        \u003c/li\u003e\n    @endforeach\n\u003c/ul\u003e\n```\n\n_You can modify the template however you want._\n\n## Translating URL slugs/parameters\n\nAdd this code to your view file inside `@php {your code here} @endphp` block or inside a controller method where you want to translate slugs:\n\n```\nforeach (config('loki.supportedLocales') as  $locale) {\n    URL::overrideParameters($locale, $model-\u003e{'slug_'.$locale});\n}\n```\n\nReplace `$model-\u003e{'slug_'.$locale}` with whatever logic you use to get the translated slug for the model from database.\n\nThis method will override the route parameters for given locale, so that the language switcher will return correct URLs with translated slugs.\n\n## Sponsors \u0026 Backers\n\nI would like to extend my thanks to the following sponsors \u0026 backers for funding my open-source journey. If you are interested in becoming a sponsor or backer, please visit the [Backers page](https://mariobasic.com/backers).\n\n## Contributing\n\nThank you for considering contributing to Loki! The contribution guide can be found [Here](https://mariobasic.com/contributing).\n\n## Code of Conduct\n\nIn order to ensure that the open-source community is welcoming to all, please review and abide by the [Code of Conduct](https://mariobasic.com/code-of-conduct).\n\n## License\n\nLoki is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravelista%2Floki","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaravelista%2Floki","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaravelista%2Floki/lists"}