orkhanahmadov/spreadsheet-translations
Composer 安装命令:
composer require orkhanahmadov/spreadsheet-translations
包简介
Spreadsheet translations for Laravel
README 文档
README
Why?
Maintaining multi-language support in Laravel applications can be hard
- Laravel's translation files are in plain PHP/JSON files. This assumes that the person who's going to translate the application knows how to work with PHP/JSON files, which is not always the case
- Each locale's translations are located in different folders. For example, the
enfolder contains English translations, and thedefolder contains German translations. This separation is good on the code level but makes it hard to maintain 2+ locale translations. It is easy to add one new key and translation for English but forget to do it in German since nothing forces this or makes it easy to spot.
Alternatively, you can store the application's translations in a spreadsheet file, something like:
| key | en | de | es |
|---|---|---|---|
| dashboard.statistics | Statistics | Statistik | Estadísticas |
| login.form.first_name | First name | Vorname | Nombre de pila |
| login.welcome | Welcome | Wilkommen | Bienvenida |
This solves all the above-mentioned problems:
- The translations maintainer does not need to know how to work with PHP or JSON
- All translations are maintained in a single file and view
- Each translation is located under the locale column, which makes it very easy to spot missing translations
But now the problem is, that Laravel cannot directly work with this spreadsheet file to display translations.
Here comes the spreadsheet-translations package!
It reads spreadsheet files that contain translations for multiple locales and generates plain JSON files out of it that Laravel can work with.
Installation
You can install the package via Composer:
composer require orkhanahmadov/spreadsheet-translations
Publish config file using:
php artisan vendor:publish --provider="Orkhanahmadov\SpreadsheetTranslations\SpreadsheetTranslationsServiceProvider"
The config file contains the following parameters:
locales- an array of locale codes that the parser should look for in the spreadsheet. The default is['en']filepath- path to a spreadsheet file. By default, points to thetranslations.xlsxfile in the Laravel project'slangdirectory. This config parameter can also use a URL as a remote file location. When a valid URL is provided parser will try to download the file to a temporary local file and parse it.sheet- defines which sheet should be used in a spreadsheet file. The default isnull. Whennull, the parser selects an active sheet in the spreadsheet to parse translations from. If you want to use a different sheet, provide the sheet's name on this parameter.header_row_number- which row should be used as header. The default is1. The header row should contain locale codes that are defined aslocalesconfig parameterkey_column- which column should be used for translation keys. The default isAcolumn.ignored_rows- an array of row numbers that should be ignored when translations are parsed. The default is an empty array.
Usage
Let's imagine we have the following Excel spreadsheet file which is located in a remote server with a public URL https://example.com/translations.xlsx.
Spreadsheet contains:
| comments | key | en | de | es |
|---|---|---|---|---|
| Dashboard statistics section title | dashboard.statistics | Statistics | Statistik | Estadísticas |
| ignore this row !!!!! | ||||
| First name field on login form | login.form.first_name | First name | Vorname | Nombre de pila |
| Welcome page title | login.welcome | Welcome | Wilkommen | Bienvenida |
We want to:
- Point parser to the remote file to download and parse
- Parse only
enanddelocale translations - Use the
keycolumn as key, in this case, columnBin the spreadsheet coordinates - Ignore row number 3
Once we publish the config file we need to make the following adjustments:
[
'filepath' => 'https://example.com/translations.xlsx', // direct download URL of the file
'locales' => ['en', 'de'], // parse `en` and `de` translations only, which means `es` will be ignored
'key_column' => 'B', // sets key column to B
'ignored_rows' => [3], // ignore row number 3
]
Package ships with an artisan command translations:generate.
php artisan translations:generate
When executed it generates necessary JSON translation files in Laravel's lang directory.
For above spreadsheet file and configuration translations:generate will generate following folder and file structure:
lang/en.json{"dashboard.statistics": "Stastitics", "login.form.first_name": "First name", "welcome": "Welcome"}
de.json{"dashboard.statistics": "Statistik", "login.form.first_name": "Vorname", "welcome": "Wilkommen"}
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
Security
If you discover any security-related issues, please email hey@orkhan.dev instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Alternatives
You can check larswiegers/laravel-translations-checker if you want to detect missing translations.
orkhanahmadov/spreadsheet-translations 适用场景与选型建议
orkhanahmadov/spreadsheet-translations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.4k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2023 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「multi language」 「spreadsheet translations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 orkhanahmadov/spreadsheet-translations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 orkhanahmadov/spreadsheet-translations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 orkhanahmadov/spreadsheet-translations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Laravel Multiauth package
Theme and asset management for laravel
Package for convenient work with Laravel's localization features
This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package.
统计信息
- 总下载量: 6.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-24