定制 sribna/laravel-csv-translation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sribna/laravel-csv-translation

Composer 安装命令:

composer require sribna/laravel-csv-translation

包简介

Store Laravel translations in CSV files

README 文档

README

Laravel CSV translation (a.k.a LCT) offers a different approach to translate strings in Laravel 6. Translations are stored in safe, non-executable CSV files so you can edit them easily with any spreadsheet editor.

Please note: this concept is experimental. Feel free to open issues and submit your PRs!

Why

By default, Laravel provides two ways of storing translations - PHP arrays and JSON strings.

Disadvantages of PHP arrays:

Executable code requires translators to properly escape characters, use valid PHP syntax, otherwise you get a fatal error. Message IDs could be a nightmare when you develop large applications.

Disadvantages of JSON

Although JSON could be considered as a safe format, translators still have to properly escape characters and deal with the non-human-friendly format. No message IDs, but now you cannot divide your translations, which means all the strings (thousands and thousands for large apps) are loaded in memory.

Introducing LCT

  • Safe, human-readable format
  • Handy. Use regular spreadsheet programs to edit translations
  • DRY. No message IDs
  • Optimized. Automatically splits translations by "context" files
  • Smart. Finds and adds untranslated strings to original files on the fly

Usage

LCT provides the following functions (and corresponding Blade directives):

Translate regular strings:

t(string $key, array $replace = [], string $context = null)

Translate plural strings:

tp(string $key, $number, array $replace = [], string $context = null)

Set global translation contex:

trans_context(string $context = null)

How it works

Let's see what happens when translating Hello :user into ukrainian language

app()->setLocale('uk'); // Set ukrainian language globally

echo t('Hello :user', ['user' => 'Юра'], 'user');

First, LCT will try to find and parse a CSV file for the current context. We have specified "user" so the expected file is resources/lang/uk/context/user.csv.

Once found and parsed, LCT checks Hello :user key in the array of translations. If key Hello :user is set, further processing is stopped and we see a localized text.

Otherwise, LCT will check "parent" file resources/lang/uk/context/_uk.csv, which is a copy of resources/lang/uk/_uk.csv (it has been copied automatically during translator initialization)

If the parent file doesn't contain Hello :user, LCT loads the distributed translation file resources/lang/uk/uk.csv (contains all translations for your app) and appends Hello :user key to resources/lang/uk/context/user.csv and resources/lang/uk/context/_uk.csv so you can translate it later using a spreadsheet editor.

As you see, specifying context in t() function can be quite tedious. You can skip it and use default route URI context or set globally (e.g per view)

Route URI context

This is by default. Say we're at page "product/some-id" which is result of this route definition:

Route::get('product/{id}', function () {
  return view('product');
});

The expected context file for this route will be resources/lang/uk/context/uri-product-id.csv.

Setting context globally

Use trans_context() in regular PHP files and @trans_context in Blade views

<?php

trans_context('user');

echo t('Hello :user', ['user' => 'Юра']); // Uses "user" context

trans_context(); // Reset context to default

echo t('Hello :user', ['user' => 'Юра']); // Uses URI context

Blade views:

@trans_context('user');

{{ t('Hello :user', ['user' => 'Юра']) }} <!-- Uses "user" context -->

@trans_context; <!-- Reset context to default -->

{{ @t('Hello :user', ['user' => 'Юра']) }} <!-- Uses URI context -->

Pluralization

Use tp() in regular PHP files and @tp() in Blade views. They are equal to Laravel's trans_choice()

Installation

composer require sribna/laravel-csv-translation

sribna/laravel-csv-translation 适用场景与选型建议

sribna/laravel-csv-translation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「laravel-localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sribna/laravel-csv-translation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sribna/laravel-csv-translation 我们能提供哪些服务?
定制开发 / 二次开发

基于 sribna/laravel-csv-translation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-21