Load Laravel localizations from a CSV File
composer require rogervila/laravel-csv-translationsTo use Laravel CSV Translations you will have to replace the Laravel TranslationServiceProvider with the package one.
// config/app.php
'providers' => [
// ...
// Illuminate\Translation\TranslationServiceProvider::class,
LaravelCSVTranslations\TranslationServiceProvider::class,
// ...
],If your project uses Illuminate\Support\ServiceProvider, replace it via the replace method.
// config/app.php
'providers' => ServiceProvider::defaultProviders()
->replace([
\Illuminate\Translation\TranslationServiceProvider::class => \LaravelCSVTranslations\TranslationServiceProvider::class,
])->merge([
// ...
])->toArray(),To make it work without modifying any configuration, Create a lang.csv file placed in the lang folder.
Translations will be loaded from the CSV file if it exists. Otherwise, Laravel's built-in translation system will handle them.
The package allows configuring some of its features.
There is no config file published by the package. You might create it to override the package defaults:
<?php
// config/lang.php
return [
'csv' => [
'enabled' => (bool) env('CSV_TRANSLATIONS_ENABLED', true),
// You might use a custom resolver to get CSV data from elsewhere
'resolver' => \LaravelCSVTranslations\CSVLocalFileResolver::class,
'throw_missing_file_exception' => false,
'cache' => [
'key' => \LaravelCSVTranslations\CSVLoader::class,
'store' => 'array',
'seconds' => 0,
],
]
];The CSV data should have keys on the first column, and then one column per locale with its ISO 639-1 code as a header.
| keys | custom column | en | es | ca |
|---|---|---|---|---|
| greetings.good_morning | Columns that do not match the current locale are just ignored | Good morning :name! | Buenos días, :name! | Bon dia, :name! |
The CSV format is quite flexible. These are some of its features:
Laravel's PHP translation array files allow having more than one dimension that can be accessed with dots.
The CSV only allows one dimension, but it allows to use dots, as shown in the CSV Format example.
While keys must be placed on the first column, its header content does not matter, so it's not necessary to name it "keys".
Except for the translation keys column, the order does not matter, so you can have N custom columns between locale columns if you need them.
Sometimes, business wants to have additional columns for translation files, like the view where a translation is placed, its context, etc.
You can have as many columns as you need, placed in the order you need.
By default, the package uses the CSVLocalFileResolver class that will try to load a lang.csv file from the project's lang path.
You may create your own CSV Resolver to customize the way to get the data:
<?php
// config/lang.php
return [
'csv' => [
'resolver' => \App\Lang\RemoteCSVFileResolver::class,
]
];
// app/Lang/RemoteCSVFileResolver.php
namespace App\Lang;
use LaravelCSVTranslations\CSVResolverInterface;
class RemoteCSVFileResolver implements CSVResolverInterface
{
public function resolve(): array
{
// Return the CSV formatted data
}
}Sometimes it is useful to access the raw data to list all available translation keys and their values.
To do so, CSVLoader comes with a handy raw method that returns an associative array with all translation keys and their raw values.
// If TranslationServiceProvider is correctly configured, 'translation.loader' should be an instance of CSVLoader
/** @var CSVLoader $loader */
$loader = $this->app['translation.loader'];
// Raw method returns an associative array with all translation keys and their raw values
$raw = $loader->raw('ca')
/*
[
"greetings.good_morning" => "Bon dia, :name!",
// ...
]
*/Created by Roger Vilà
This package is open-sourced software licensed under the MIT license.
Package icon made by Freepik - Flaticon
