nikaia/translation-sheet

Laravel 通过 Google Spreadsheet 进行翻译

v1.7.1 2024-05-12 15:04 UTC

README

使用 Google Spreadsheet 翻译 Laravel 语言文件。

Latest Version on Packagist Build Status

内容

安装

  • 安装包

    $ composer require nikaia/translation-sheet --dev
  • 如果 Laravel 版本 <= 5.4,请将服务提供者添加到您的 'config/app.php' 文件中。对于版本 >= 5.5,Laravel 将自动发现该包。

    Nikaia\TranslationSheet\TranslationSheetServiceProvider::class,
  • 配置可以通过环境变量完成,但如果您愿意,您可以通过使用以下方法覆盖配置,发布包配置文件:

    $ php artisan translation_sheet:publish
        or
    $ php artisan vendor:publish --provider="Nikaia\TranslationSheet\TranslationSheetServiceProvider"

要求

Laravel >= 5.1

配置

Google API 凭据

  • https://console.developers.google.com/ 创建一个新的项目
  • 确保为创建的项目激活 Sheet Api
    • 导航到 "Library"
    • 搜索 "Google Sheets API" > 点击 "Google Sheets API" 并点击启用
  • 创建凭据
    • 导航到 "Credentials"
    • 点击 "Create credentials"
    • 选择 "Service Account key"
    • 在 "Service account" 选择中创建一个新的 "New Service Account"
      • 选择一个名称。(例如,这是将显示在电子表格历史记录操作中的名称),
      • 选择 "Editor" 作为角色
      • 选择 "JSON" 作为密钥类型。
    • 将凭据保存到 'resources/google/service-account.json' 文件夹中。(您可以选择另一个名称/文件夹并更新包配置)
    • 请确保记录服务帐户电子邮件,您需要将电子表格与该电子邮件共享(见下文)。

电子表格

包配置

在您的 .env 文件或已发布的配置文件(config/translation_sheet.php)中,您需要添加以下内容

# The service account email   
TS_SERVICE_ACCOUNT_EMAIL=***@***.iam.gserviceaccount.com

# The path to the downloaded service account credentials file
TS_SERVICE_ACCOUNT_CREDENTIALS_FILE=resources/google/service-account.json

# The ID of the spreadsheet that we will be using for translation (the last portion of the spreadsheet url)
TS_SPREADSHEET_ID=xxxx

# The locales of the application (separated by comma) 
TS_LOCALES=fr,en,es

用法

1/ 设置电子表格

此操作只需进行一次。

$ php artisan translation_sheet:setup

2/ 准备表格

为了避免一些冲突,我们将首先运行此命令来重写区域语言文件。

$ php artisan translation_sheet:prepare

3/ 将翻译发布到表格

$ php artisan translation_sheet:push

4/ 将电子表格与客户或项目经理共享以进行翻译。

5/ 完成后,您可以在电子表格上锁定翻译(以避免冲突)

$ php artisan translation_sheet:lock

6/ 拉取翻译

这将从电子表格中拉取翻译,并将其写入应用程序的语言文件。您可以使用 git diff 确保一切正常(冲突、错误等 ...)

$ php artisan translation_sheet:pull

6/ 解锁电子表格上的翻译

$ php artisan translation_sheet:unlock

在浏览器中打开电子表格

$ php artisan translation_sheet:open

排除翻译

有时您可能需要指示包排除某些翻译。您可以通过指定 exclude 配置选项中的模式来完成此操作。它接受多个模式,针对完整的翻译密钥,且 Str::is 可以理解。

[
    // ...
    
    'exclude' => [
        'validation*',  // This will exclude all the `validation.php` translations.
        'foo::*',       // This will exclude all the `foo` namespace translations.
        'foo::bar.*',   // this will exclude the `bar` translations from the `foo` namespace.
    ],
    
    // ...
]

额外的工作表

Extra sheets

有时您可能需要翻译其他文件。它们与Laravel应用程序本身无关,并且不存储在resources\lang文件夹中。也许您正在构建一个Web应用(spa),甚至与Laravel应用程序同时构建一个移动应用,并且您需要处理它们的翻译。

在这种情况下,您可以配置额外的表来处理存储在特定路径中的这些翻译文件。

  • 此功能仅处理json文件。
  • 文件必须位于resources\lang目录之外。例如resources\web-app\lang
[
    // ...

    'extra_sheets' => [
        [
            'name' => 'Web App',                        // Spreadsheet sheet (tab) name.
            'path' => resource_path('web-app/lang'),    // Path where json files are stored. Files can be organized inside sub folders.        
            'tabColor' => '#0000FF',                    // Color of the spreadsheet tab
        ],
        [
            'name' => 'Mobile App',
            'path' => resource_path('mobile-app/lang'),        
            'tabColor' => '#0000FF',
        ],    
    ],
]

如果您稍后添加此配置,则需要运行translation_sheet:setup命令。

变更日志

有关最近更改的更多信息,请参阅变更日志

测试

$ composer test

注意:运行测试需要有效的配置服务文件service-account.json。

Github动作

要使用GitHub动作测试您的分支,您需要一个有效的service-account.json文件。该文件在存储库中被忽略,以避免泄露凭证。您需要使用`gpg`对凭证文件tests/fixtures/service-account.json进行编码

# Save credential file to tests/fixtures/service-account.json
$ gpg -c tests/fixtures/service-account.json  tests/fixtures/service-account.json.gpg

提交编码后的.gpg文件。

PS. GitHub动作将在运行测试前解密文件。请参阅run-tests.yml文件。

安全

如果您发现任何安全问题,请通过电子邮件nbourguig@gmail.com联系,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅贡献指南

致谢