rogervila/laravel-csv-translations

Load Laravel localizations from a CSV File

2.3.0 2024-04-16 06:03 UTC

README

Laravel CSV Translations

Build Status StyleCI Quality Gate Status

Latest Stable Version Total Downloads License

Laravel CSV Translations

Load Laravel localizations from a CSV File

Installation

composer require rogervila/laravel-csv-translations

To 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.

Configuration

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,
        ],
    ]
];

CSV format

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.

CSV features

The CSV format is quite flexible. These are some of its features:

Dimensions

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.

Translation keys column

While keys must be placed on the first column, its header content does not matter, so it's not necessary to name it "keys".

Column order

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.

Custom columns

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.

CSV data resolver

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
    }
}

Access raw 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!",
  // ...
]
*/

Author

Created by Roger Vilà

License

This package is open-sourced software licensed under the MIT license.

Package icon made by Freepik - Flaticon