alive2tinker/transwire

翻译包,用于在项目中搜索可翻译的字符串,并将它们列在{locale}.json文件中,以便翻译

dev-main 2021-07-23 14:54 UTC

This package is auto-updated.

Last update: 2024-09-23 21:30:23 UTC


README

在多语言Laravel项目中,翻译一直是痛点。无论是字符串缺失还是编码时必须不断切换语言,工作流程似乎总是有点混乱。此包提供了一种方法,让您可以在单一语言中进行编码,当需要翻译时,只需运行命令,所有字符串都将被翻译或至少列在{locale}.json文件中,等待翻译

安装

您可以使用Composer安装此包

composer require alive2tinker/transwire

此包将自动注册服务提供者。

接下来,您需要发布WebSocket配置文件

php artisan vendor:publish --provider="alive2tinker\TransWire\Providers\TransWireServiceProvider"

这是作为config/transwire.php发布的配置文件的默认内容

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Locales
    |--------------------------------------------------------------------------
    |
    | This value is the list of locales that your application will support
    |
    */
    'locales' => [
        'en',
        'ar',
        // 'es',
        // 'fr'
    ],

    /*
    |--------------------------------------------------------------------------
    | Directories
    |--------------------------------------------------------------------------
    |
    | This value is the list of directories where files with translatable strings are to be found
    | update them as per your needs.
    |
    */

    'directories' => [
        './resources/views',
        './resources/js/components',
        './app/Http/Controllers'
    ],

    /*
    |--------------------------------------------------------------------------
    | extensions
    |--------------------------------------------------------------------------
    |
    | This value is the list of extensions with corresponding regex to look for transalatable strings. 
    | you can add as many extensions as you want, just be sure to add the corresponding regex carefully
    |
    */

    'extensions' => [
        '.blade.php' => '/\@lang\(\'([A-Za-z .]*)\'\)|__\(\'([A-Za-z .]*)\'\)/',
        '.vue' => '/\{\{\s*\$t\(\'([A-z ]*)\'\)\s*}}/',
        '.php' => '/__trans\(\'([A-z 0-9 .<?>]*)/'
    ]
];

用法

完成前端工作后,无论是Blade还是Vue,甚至是后端工作,只需运行以下命令

php artisan transwire:translate

此命令将提取所有待翻译的字符串,并将它们存储在./resources/lang目录下的{locale}.json文件中

路线图

  • 初始功能正常运行
  • 从服务提供商(如Google翻译或Microsoft翻译)翻译字符串。或实现LibreTranslate服务

许可证

MIT