codewiser/polyglot

Laravel应用程序的翻译字符串解析器和网页编辑器

2.0.7 2024-09-28 15:29 UTC

README

简介

Polyglot提供了一个漂亮的翻译编辑器,并可以从应用程序源代码中提取翻译字符串。

Editor dashboard

使用Polyglot,您可以确保应用程序完全本地化。

安装

在您的服务器上安装Gettext,并确保php已启用ext-gettext扩展。

现在,您可以使用Composer包管理器将Polyglot安装到您的项目中。

composer require codewiser/polyglot

安装Polyglot后,使用polyglot:install Artisan命令发布其资产。

php artisan polyglot:install

配置

发布Polyglot的资产后,其主要的配置文件将位于config/polyglot.php。此配置文件允许您配置Polyglot的工作模式。每个配置选项都包括其用途的说明,因此请务必仔细研究此文件。

升级Polyglot

在升级到任何新版本的Polyglot时,您应重新发布Polyglot的资产。

php artisan polyglot:publish

为了保持资产的最新状态并避免未来的更新中出现问题,您可以在应用程序的composer.json文件中的post-update-cmd脚本中添加polyglot:publish命令。

{
    "scripts": {
        "post-update-cmd": [
            "@php artisan polyglot:publish --ansi"
        ]
    }
}

网页编辑器

File browser

Strings

Editor

配置

仪表板授权

Polyglot在/polyglot URI上公开了一个仪表板。默认情况下,您只能在本地环境中访问此仪表板。

不建议在非本地环境中使用Polyglot,因为Polyglot会修改resources/lang中的文件。

然而,在您的app/Providers/PolyglotServiceProvider.php文件中,有一个授权门定义。此授权门控制非本地环境中对Polyglot的访问。您可以按需修改此门以限制对Polyglot安装的访问。

/**
 * Register the Polyglot gate.
 *
 * This gate determines who can access Polyglot in non-local environments.
 *
 * @return void
 */
protected function gate()
{
    Gate::define('viewPolyglot', function ($user) {
        return in_array($user->email, [
            'username@example.com',
        ]);
    });
}
替代认证策略

请记住,Laravel会自动将认证用户注入到门闭包中。如果您的应用程序通过其他方法提供Polyglot安全,例如IP限制,则Polyglot用户可能不需要“登录”。因此,您需要将上面的function ($user)闭包签名更改为function ($user = null),以强制Laravel不需要身份验证。

字符串收集器

配置

定义至少一组源文件以从中收集字符串。

'sources' => [
    [
        'include' => [
            app_path(),
            resource_path('views')
        ],
        'exclude' => [],
    ]
],

应用程序区域设置

Gettext依赖于服务器对区域设置的 support。例如,您的应用程序提供意大利语(it)。并且您的服务器支持以下区域设置

> locale -a | grep it

it_CH
it_CH.utf8
it_IT
it_IT@euro
it_IT.utf8

然后您应按优先级顺序列出系统区域设置。更多信息请参阅https://php.ac.cn/manual/en/function.setlocale.php

'locales' => [
    'en' => ['en_US', 'en_US.utf8', 'en_US.UTF-8'] 
    'it' => ['it_IT', 'it_IT.utf8', 'it_IT.UTF-8'], 
    'es' => ['es_ES', 'es_ES.utf8', 'es_ES.UTF-8'],
],

收集字符串后,Polyglot将通过每个配置的区域设置填充收集到的字符串。

收集字符串

一旦您在应用程序的config/polyglot.php配置文件中配置了sources,您就可以使用polyglot Artisan命令收集字符串。此单个命令将收集所有配置源中的翻译字符串。

php artisan polyglot:collect

Polyglot使用sources来收集翻译字符串,理解transtrans_choice@trans和其他Laravel特定指令。

收集字符串后,您的应用程序的 resourse/lang 文件夹可能看起来像这样:

resources/
  lang/
    es/
      auth.php
      passwords.php
    en/
      auth.php
      passwords.php
    it/
      auth.php
      passwords.php
    es.json
    en.json
    it.json

您只需翻译文件。

加载字符串

Polyglot 提供了 AcceptLanguage 中间件,这可能有助于设置适当的区域设置。

class AcceptLanguage
{
    public function handle(Request $request, Closure $next)
    {
        app()->setLocale($request->getPreferredLanguage(Polyglot::getLocales()));

        return $next($request);
    }
}

Gettext翻译器

在阅读之前,您应该熟悉 Gettext

配置

设置环境变量 POLYGLOT_GETTEXT=true 以使用 Gettext 本地化您的应用程序。

'enabled' => env('POLYGLOT_GETTEXT', true),

文本域

您可以通过这种方式配置额外的源文件组

'sources' => [
  [
    'text_domain' => 'frontend',
    'include' => [
        app_path(),
        resource_path('views'),
    ],
    'exclude' => resource_path('views/admin'),
  ],
  [
    'text_domain' => 'backend',
    'include' => [
        resource_path('views/admin'),
    ],
    'exclude' => [],
  ],
],

text_domain 的默认值是字符串 messages

收集字符串

运行 polyglot:collect Artisan 命令后,您的应用程序的 resourse/lang 文件夹可能看起来像这样:

resources/
  lang/
    es/
      LC_MESSAGES/
        backend.po
        frontend.po
    en/
      LC_MESSAGES/
        backend.po
        frontend.po
    it/
      LC_MESSAGES/
        backend.po
        frontend.po

编译字符串

生成的文件包含收集到的字符串,您可能需要将其翻译。翻译完成后,您应该将所有 po 文件编译为 Gettext 可理解的 mo 格式。使用 Artisan 命令进行编译。

php artisan polyglot:compile

每个 po 文件旁边都会出现一个 mo 文件。

请记住,PHP 会缓存 mo 文件的内容。因此,在编译后,请确保您已重新启动了 Web 服务器。

向后兼容性

即使使用 Gettext 驱动,您也可以继续使用 Laravel 翻译指令,如 transtrans_choice

同时,您可以使用 Gettext 指令,如 gettextngettext 等。

它们都为 Polyglot 可理解。

加载文本域

默认情况下,Polyglot 将第一个配置的文本域加载到 PHP 内存中。

如果您配置了多个文本域,您可以通过访问 Laravel 的 Lang 门面来加载下一个文本域。

Lang::setTextDomain('frontend');

支持指令

Polyglot 支持以下 Gettext 指令。

在当前文本域中查找消息

gettext(string $message): string

gettext 的复数版本

ngettext(string $singular, string $plural, int $count): string

特定版本的 gettext 允许定义上下文

pgettext(string $context, string $message): string

特定版本的 ngettext。

npgettext(string $context, string $singular, string $plural, int $count): string

其他允许覆盖当前文本域和类别的指令也得到支持。

Gettext 的力量

Editor

Gettext 对于翻译者非常有帮助。使用以下食谱以良好的本地化完成。

参考

Gettext 提取字符串的引用,以便翻译者可以假设上下文。

#: /sources/php/second.php:3 /sources/js/first.js:1
msgid "Short message"
msgstr ""

开发者注释

Gettext 可以提取开发者注释,这可能对翻译者有帮助。

#. The message will be shown at test page only.
msgid "Hello world"
msgstr ""

它起源于这样的源代码

// The message will be shown at test page only.
echo gettext('Hello world');

消息上下文

开发者可以显式定义消息上下文。

gettext('May');
pgettext('Month', 'May');

这里有两条消息具有相同的 msgid 但不同的 msgctxt,实际上是字符串键的一部分。

msgid "May"
msgstr ""

msgctxt "Month"
msgid "May"
msgstr ""

翻译者注释

在编辑字符串时,翻译者可以留下一个或多个注释。这些注释可能对未来的翻译者有帮助。

# They say it was about posibilities...
msgid "May"
msgstr ""

模糊字符串

Gettext(在解析源代码时)和翻译者都可能将字符串标记为模糊。这意味着之前位于该位置的字符串已被更改,因此当前的翻译可能不再适用。

#, fuzzy
msgid "May"
msgstr ""

Vue 和 JavaScript 支持

也支持 Vue 和 JavaScript 源。

编译字符串

Artisan 的 polyglot:compile 命令将编译每个翻译文件到 json 格式,并将它们放入 storage 文件夹。编译后,storage/lang 可能看起来像这样:

storage/
  lang/
    es/
      frontend.json
    en/
      frontend.json
    it/
      frontend.json

您可以使用这些文件来本地化前端。

Polyglot 提供了 translations.js,您的应用程序可以使用它作为混合。Polyglot 仪表板就是这样本地化的。