centrust / nova-localization
Laravel Nova Localization Package
Requires
- php: ^8.1
- illuminate/contracts: ^10.0|^11.0
- laravel/nova: ^4.0
- outl1ne/nova-inline-text-field: ^2.0
- spatie/laravel-package-tools: ^1.14.0
- spatie/nova-translatable: ^3.1|^4.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.20
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
README
This package is tailored for developers using Laravel Nova who need an easy, reliable solution for managing their application's translations.
Currently Only Supports English and Arabic languages.You are free to fork and add your own language support.
Features
- Inline Translation: Automatically translates your application's text from the dashboard.
- Flexibility: Designed to work seamlessly with Laravel's built-in localization functionality.
- Easiness: An easy configuration process ensures you can start using instantly.
Example Use
in Nova Resource
Text::make(_tran('Name'), 'name'), Text::make(_tran('Description'), 'description')
Result
-
ِِArabic الأسم
-
English Name
-
ِِArabic التعريف
-
English Description
Installation
You can install the package via composer:
composer require centrust/nova-localization
You can publish and run the migrations with:
php artisan vendor:publish --tag="nova-localization-migrations"
php artisan migrate
Once you Migrate the table, you need to add 'locale' column to your User Model fillable array.
You can publish the config file with:
php artisan vendor:publish --tag="nova-localization-config"
This is the contents of the published config file:
return [ /** * If set to true, this will enable caching for localization. The actual value for this translation is taken from the server's cache. * If the LOCALIZATION_ENABLE_CACHE environment variable does not exist, the default value will be false. * This is useful for improving the application's performance by caching localizations, * but it might delay the appearance of any changes made to localization files until the cache is refreshed. **/ 'localization_enable_cache' => env('LOCALIZATION_ENABLE_CACHE', false), /** * This URL is for loading the Arabic font from Google Fonts. * This font is needed for Arabic language support in your application. **/ 'ar_google_font_url'=> 'https://fonts.googleapis.com/css2?family=Scheherazade+New:wght@700&display=swap', /** * This is the path to the CSS file that contains the Arabic font family. * example : 'css/rtl-ar.css' * The file must be in the public folder. * This font is needed for Arabic language support in your application. */ 'ar_font_family_css'=> 'css/rtl-ar.css', ];
Example for the CSS file that contains the Arabic font family.
body { font-family: 'Scheherazade New', 'Nunito Sans', 'Nunito', sans-serif; }
Usage
To translate any text in your application, you can use the _tran() helepr function:
_tran('This is a label');
To show the Localization Resource in the menu, (I havent figured out how to show it automatically yet) but you can add it manually by Creating your own normal resource and extend this package resource
use Centrust\NovaLocalization\Nova\NovaLocalizationResource; class Localization extends NovaLocalizationResource { }
To have the user switch languages in the User menu, you can add this to your User Menu in the Novaserviceprovider.php
Nova::userMenu(function (Request $request, Menu $menu) { $language = app()->getLocale() == 'ar' ? 'en' : 'ar'; $menu->append(MenuItem::externalLink(app()->getLocale() == 'ar' ? 'English' : 'عربي', '/change-language/' . $language . '/' . $request->user()->id)); return $menu; });
This part will switch the locale and to RTL if 'Arabic' is selected
How It works
- When you use the _tran() function, which is a global helper function, available in all your application's files,
- the package will search for the translation in the database,
- if it does not find it, it will search for it in the Laravel language files.
- If it does not find it in the language files, it will save it in the database and return it to you.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.