namnhp-2137/translation-sheet

Laravel通过Google表格进行翻译

v1.4.5 2020-09-25 14:56 UTC

README

使用Google表格进行Laravel语言文件的翻译。

Latest Version on Packagist Build Status Quality Score

内容

安装

  • 安装包

    $ 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/ 创建一个新项目
  • 确保为创建的项目激活 表单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文件。

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

安全性

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

贡献

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

致谢