定制 rpwebdevelopment/laravel-translate 二次开发

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

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

rpwebdevelopment/laravel-translate

Composer 安装命令:

composer require rpwebdevelopment/laravel-translate

包简介

Package for generating translated language files

README 文档

README

Packagist Version Packagist Downloads License: MIT

Laravel Translate is a tool intended to automatically generate translated language files. Currently, the package leverages either DeepL API, Amazon Translate or the amazing Google Translate package from Stichoza which allows for zero configuration usage.

The default configuration makes use of Google translations and can be used straight out of the box with no sign-up or configuration. If you would rather use the DeepL translations API you will need to sign up here; DeepL configuration details described below.

Installation

You can install the package via composer:

composer require rpwebdevelopment/laravel-translate

You can publish the config file with:

php artisan vendor:publish --tag="translate-config"

This is the contents of the published config file:

return [
    'reader' => 'php',
    'default_source' => 'en_GB',
    'target_locales' => [],
    'lang_directory' => base_path('resources/lang'),
    'provider' => 'google',
    'providers' => [
        'google' => [
            'package' => GoogleTranslate::class,
        ],
        'deepl' => [
            'package' => DeeplTranslate::class,
            'token' => env('DEEPL_AUTH_TOKEN', null),
            'model_type' => 'prefer_quality_optimized',
            'formality' => 'default',
        ],
        'aws' => [
            'package' => AwsTranslate::class,
            'credentials' => [
                'key' => env('AWS_ACCESS_KEY_ID', null),
                'secret' => env('AWS_SECRET_ACCESS_KEY', null),
            ],
            'settings' => [
                "Formality" => "FORMAL"
            ],
            "region" => env('AWS_DEFAULT_REGION', "us-east-1"),
            "version" => "latest",
        ],
    ],
    'readers' => [
        'php' => PhpReader::class,
        'json' => JsonReader::class,
    ],
    'writers' => [
        'php' => PhpWriter::class,
        'json' => JsonWriter::class,
    ],
];

DeepL Configuration

In order to make use of the DeepL API you will need to publish the config file as detailed above and update the provider to deepl. You will then need to add your DeepL API auth token to your .env file under the env variable DEEPL_AUTH_TOKEN.

Amazon Configuration

In order to make use of the AWS Translate API you will need to publish the config file as detailed above and update the provider to aws. You will then need to ensure that you .env file contains the required AWS variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY & AWS_DEFAULT_REGION.

Note:

You will need to ensure that the AWS IAM role has the permission policy TranslateFullAccess .

Defining Locales

When configuring target languages it is important to follow the below conventions:

'target_locales' => [
    [FILE/DIR PATH] => [
        'lang' => [LANG CODE],
        'formality' => [OPTIONAL FORMALITY]
    ]
],

Example:

'target_locales' => [
    'de_DE_formal' => [
        'lang' => 'de_DE',
    ],
    'de_DE_casual' => [
        'lang' => 'de_DE',
        'formality' => 'less_formal',
    ],
]

The above would output two different German language translations; one using more formal language and one using less formal language

Note:

Formality is currently only available for DeepL and AWS translations. This setting will have no impact when using Google translate.

Usage

This package is designed to function with as much flexibility as possible, as such it is designed to work for multiple configurations with minimal setup, whether you have a single file or directory per language, or if you are using PHP lang files or JSON lang files.

php artisan laravel-translate {target} {--source=?} {--file=?} {--missing-only}

Options:

Command Required Description
target required Set locale/language to be translated into.
--source= optional Set locale/language source being translated from.
--file= optional Set specific filename being translated from & into.
--missing-only optional Flag only missing values need to be translated/added.

Bulk Processing

Rather than processing each target language manually you can add all your target locales to the target_locales option in the translations configuration file, this will enable the usage of the bulk translate command:

php artisan laravel-translate:bulk {--source=?} {--file=?} {--missing-only}

Options:

Command Required Description
--source= optional Set locale/language source being translated from.
--file= optional Set specific filename being translated from & into.
--missing-only optional Flag only missing values need to be translated/added.

Key Validation

If you simply wish to ascertain if translations are missing from your project you can implement the missing command:

php artisan laravel-translate:bulk {--verbose}

Options:

Command Required Description
--verbose optional Flag to indicate if full details of missing keys are required.

This command can be used in conjunction with git-hooks in order to prevent changes being deployed to your repository with missing translations, this can be done by adding the following bash script to your .git/hooks/pre-commit:

#! /bin/bash

missing="$( php artisan laravel-translate:missing )"

if [ "$missing" = "translations missing" ];then
  echo "Translations Missing";
  exit 0
fi

Example Usages

Directory Structure:

Example directory structure:

resources 
|----lang
     |----en_GB
          |----auth.php
          |----global.php

Assuming the config default_source is set to en_GB; for the above structure the following command:

php artisan laravel-translate fr_FR

Would produce the following structure:

resources 
|----lang
     |----en_GB
     |    |----auth.php
     |    |----global.php
     |----fr_FR
          |----auth.php
          |----global.php

Alternately, you can explicitly declare the source locale:

php artisan laravel-translate fr_FR --source=en_GB

If you are only wanting to replace a specific file you can also pass in the file option (note the file extension is optional):

php artisan laravel-translate fr_FR --source=en_GB --file=auth

Single File Structure

If you are using a single file per language rather than a directory structure as below:

resources 
|----lang
     |----en_GB.php
     |----fr_FR.php

You can use the exact same command syntax, so the following:

php artisan laravel-translate de_DE --source=en_GB

Would result in the following output:

resources 
|----lang
     |----en_GB.php
     |----fr_FR.php
     |----de_DE.php

Notes

  • Any existing translations will be overridden, it is recommended that you back-up any previously created lang files to allow for easy restoration if required.

  • Translations are a result of machine translation and therefore some translation errors may occur.

  • While efforts have been made to persist lang string attributes, their persistence cannot be 100% guaranteed, as such it is recommended to verify your files after production.

Credits

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

rpwebdevelopment/laravel-translate 适用场景与选型建议

rpwebdevelopment/laravel-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 781 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-27