├── .styleci.yml ├── config └── config.php ├── src ├── Macros │ └── AppMacros.php ├── LaravelLocalesServiceProvider.php └── helpers.php ├── .github └── workflows │ └── tests.yml ├── LICENSE.md ├── CHANGELOG.md ├── composer.json ├── README.md └── CONTRIBUTING.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | [ 14 | 'en', 15 | ], 16 | ]; 17 | -------------------------------------------------------------------------------- /src/Macros/AppMacros.php: -------------------------------------------------------------------------------- 1 | $locales, 18 | 'locales.supported' => $locales, 19 | ]); 20 | } 21 | 22 | $locales = config('app.locales') ?? config('locales.supported'); 23 | 24 | return isset($locales[0]) ? $locales : array_keys($locales); 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/LaravelLocalesServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 17 | $this->publishes([ 18 | __DIR__.'/../config/config.php' => config_path('locales.php'), 19 | ], 'config'); 20 | } 21 | } 22 | 23 | /** 24 | * Register the application services. 25 | */ 26 | public function register() 27 | { 28 | $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'locales'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | tests: 6 | name: PHP ${{ matrix.php }} - ${{ matrix.stability }} 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | fail-fast: true 11 | matrix: 12 | php: ['8.0', '8.1', '8.2'] 13 | stability: [prefer-lowest, prefer-stable] 14 | 15 | steps: 16 | - name: Checkout code 17 | uses: actions/checkout@v2 18 | 19 | - name: Setup PHP 20 | uses: shivammathur/setup-php@v2 21 | with: 22 | php-version: ${{ matrix.php }} 23 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, memcached 24 | tools: composer:v2 25 | coverage: none 26 | 27 | - name: Install dependencies 28 | uses: nick-invision/retry@v1 29 | with: 30 | timeout_minutes: 5 31 | max_attempts: 5 32 | command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress 33 | 34 | - name: Execute tests 35 | run: vendor/bin/phpunit --colors --verbose 36 | -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | $locales, 15 | 'locales.supported' => $locales, 16 | ]); 17 | } 18 | 19 | $locales = config('app.locales') ?? config('locales.supported'); 20 | 21 | return isset($locales[0]) ? $locales : array_keys($locales); 22 | } 23 | } 24 | 25 | if (! function_exists('locale')) { 26 | /** 27 | * Retrieve the current locale of the application. 28 | * 29 | * @param string $locale 30 | * @return string 31 | */ 32 | function locale(?string $locale = null): string 33 | { 34 | if (! is_null($locale) && in_array($locale, locales())) { 35 | app()->setLocale($locale); 36 | } 37 | 38 | return app()->getLocale(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Chin Leung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-locales` will be documented in this file. 4 | 5 | ## [v1.1.0 - 2019-09-07](https://github.com/chinleung/laravel-locales/compare/v1.0.5...v1.1.0) 6 | 7 | - Added support for Laravel 6 8 | 9 | ## [v1.0.5 - 2019-09-03](https://github.com/chinleung/laravel-locales/compare/v1.0.4...v1.0.5) 10 | 11 | - Allow associative arrays for locales configuration ([#2](https://github.com/chinleung/laravel-locales/issues/2)) 12 | 13 | ## [v1.0.4 - 2019-08-06](https://github.com/chinleung/laravel-locales/compare/v1.0.3...v1.0.4) 14 | 15 | - Allow `locales` to set the supported locales 16 | 17 | ## [v1.0.3 - 2019-08-05](https://github.com/chinleung/laravel-locales/compare/v1.0.2...v1.0.3) 18 | 19 | - Changed the config file from `laravel-locales.php` to `locales.php` 20 | - Fixed the publish config command in the `README.md` 21 | 22 | ## [v1.0.2 - 2019-08-01](https://github.com/chinleung/laravel-locales/compare/v1.0.1...v1.0.2) 23 | 24 | - Fixed the namespace of classes. 25 | 26 | ## [v1.0.1 - 2019-08-01](https://github.com/chinleung/laravel-locales/compare/v1.0.0...v1.0.1) 27 | 28 | - Changed the default supported locales to `['en']` only 29 | - Added [locale](https://github.com/chinleung/laravel-locales#locale--string) helper 30 | 31 | ## [v1.0.0 - 2019-07-29] 32 | 33 | - Initial release 34 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chinleung/laravel-locales", 3 | "description": "Add configurations and helpers for a multi locale application.", 4 | "keywords": [ 5 | "chinleung", 6 | "laravel-locales" 7 | ], 8 | "homepage": "https://github.com/chinleung/laravel-locales", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Chin Leung", 14 | "email": "chinleung94@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0|^8.1", 20 | "illuminate/support": "^9.0|^10.0|^11.0|^12.0" 21 | }, 22 | "require-dev": { 23 | "orchestra/testbench": "^7.0|^10.0", 24 | "phpunit/phpunit": "^9.0|^11.5.3" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "ChinLeung\\LaravelLocales\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "ChinLeung\\LaravelLocales\\Tests\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit", 38 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 39 | }, 40 | "config": { 41 | "sort-packages": true 42 | }, 43 | "extra": { 44 | "laravel": { 45 | "providers": [ 46 | "ChinLeung\\LaravelLocales\\LaravelLocalesServiceProvider" 47 | ], 48 | "aliases": { 49 | "LaravelLocales": "ChinLeung\\LaravelLocales\\LaravelLocalesFacade" 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Locales 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/chinleung/laravel-locales.svg?style=flat-square)](https://packagist.org/packages/chinleung/laravel-locales) 4 | [![Build Status](https://github.com/chinleung/laravel-locales/workflows/tests/badge.svg?branch=v2)](https://github.com/chinleung/laravel-locales/actions?query=workflow%3Atests) 5 | [![Quality Score](https://img.shields.io/scrutinizer/g/chinleung/laravel-locales.svg?style=flat-square)](https://scrutinizer-ci.com/g/chinleung/laravel-locales) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/chinleung/laravel-locales.svg?style=flat-square)](https://packagist.org/packages/chinleung/laravel-locales) 7 | 8 | Add configurations and helpers to make an application support multiple locales. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ```bash 15 | composer require chinleung/laravel-locales 16 | ``` 17 | 18 | ## Configuration 19 | 20 | By default, the application locales is only going to be `en`. If your application support other locales, you can either set a `app.locales` in your `config/app.php` or publish the configuration file: 21 | 22 | ``` bash 23 | php artisan vendor:publish --provider="ChinLeung\LaravelLocales\LaravelLocalesServiceProvider" --tag="config" 24 | ``` 25 | 26 | ## Helpers 27 | 28 | ### locale(string $locale = null) : string 29 | 30 | > Retrieve or update the current locale of the application. 31 | 32 | ```php 33 | // Alias of app()->getLocale(); 34 | locale(); // 'en' 35 | 36 | // Alias of app()->setLocale('fr'); 37 | locale('fr'); // 'fr' 38 | locale(); // 'fr' 39 | ``` 40 | 41 | ### locales(array $locales = null) : array 42 | 43 | > Retrieve or update the supported locales of the application. 44 | > Has priority for `app.locales` over `laravel-locales.supported`. 45 | 46 | ``` php 47 | locales(); // ['en'] 48 | 49 | locales(['en', 'fr', 'zh']); 50 | locales(); // ['en', 'fr', 'zh'] 51 | 52 | locales(['en', 'zh']); 53 | locales(); // ['en', 'zh'] 54 | ``` 55 | 56 | ### Testing 57 | 58 | ``` bash 59 | composer test 60 | ``` 61 | 62 | ### Changelog 63 | 64 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 65 | 66 | ## Contributing 67 | 68 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 69 | 70 | ### Security 71 | 72 | If you discover any security related issues, please email hello@chinleung.com instead of using the issue tracker. 73 | 74 | ## Credits 75 | 76 | - [Chin Leung](https://github.com/chinleung) 77 | - [All Contributors](../../contributors) 78 | 79 | ## License 80 | 81 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 82 | 83 | ## Laravel Package Boilerplate 84 | 85 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 86 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | --------------------------------------------------------------------------------