rogervila/laravel-csv-translations
Composer 安装命令:
composer require rogervila/laravel-csv-translations
包简介
Load Laravel localizations from a CSV File
README 文档
README
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.
| 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! |
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
rogervila/laravel-csv-translations 适用场景与选型建议
rogervila/laravel-csv-translations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.33k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel csv translations」 「laravel csv localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rogervila/laravel-csv-translations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rogervila/laravel-csv-translations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rogervila/laravel-csv-translations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Deepl Automated translations for Laravel lang files
laravel facade to read/write csv file
Collection of tools to use the full power of the Enyalius framework
Adds support for dynamic translations from Google Spreadsheets.
A Laravel package for multilingual models
Alfabank REST API integration
统计信息
- 总下载量: 4.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-03