enriquejlicona/js-localization

Laravel 包,用于为 JavaScript 代码提供本地化。

1.0.0 2022-03-05 00:02 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Total Downloads

这是一个简单、易于使用且灵活的 Laravel 网络框架包。它允许你在 JavaScript 代码中使用 Laravel 网络应用的本地化消息(参见 resources/lang 目录)。你可以轻松配置需要导出的消息。

⚠️ 正在寻找新的维护者。如果你对此感兴趣,请与我联系。

分支

安装

将以下行添加到你的 Laravel 网络应用的 composer.json 文件的 require 部分

    "require": {
        "andywer/js-localization": "dev-laravel-6"      // see table above
    }

运行 composer update 以安装此包。

最后,将以下行添加到你的 app/config/app.php 文件的 providers 数组中

    'providers' => [
        /* ... */
        JsLocalization\JsLocalizationServiceProvider::class
    ]

配置

首先运行 php artisan vendor:publish。此命令将包的默认配置复制到 config/js-localization.php

现在你可以编辑此文件,以定义你的 JavaScript 代码中需要的消息。只需编辑配置文件中的 messages 数组。

示例(导出所有提醒消息)

<?php

return [
    // Set the locales you use
    'locales' => ['en'],

    // Set the keys of the messages you want to use in javascript
    'messages' => [
        'passwords' => [
            'password', 'user', 'token'
        ]
    ],

    /*
     * in short:
     * 'messages' => ['passwords']
     *
     *
     * you could also use:
     *
     * 'messages' => [
     *     'passwords.password',
     *     'passwords.user',
     *     'passwords.token'
     * ]
     */
     
    // Set the keys of config properties you want to use in javascript.
    // Caution: Do not expose any configuration values that should be kept privately!
    'config' => [
        'app.debug'
    ],
     
    // Disables the config cache if set to true, so you don't have to run `php artisan js-localization:refresh`
    // each time you change configuration files.
    // Attention: Should not be used in production mode due to decreased performance.
    'disable_config_cache' => false,

    // Split up the exported messages.js file into separate files for each locale.
    // This is to ensue faster loading times so one doesn't have to load translations for _all_ languages.
    'split_export_files' => true,
];

重要

当第一次使用 JsLocalizationController 时,消息配置将被缓存。在更改消息配置后,你需要调用 php artisan js-localization:refresh 来刷新缓存。这也影响你导出到 JavaScript 的配置属性,因为它们也会被缓存。

使用方法

JavaScript 的翻译资源可以是 Laravel 应用在运行时提供,也可以作为静态 JavaScript 文件预先生成,这样你可以直接从你的网络服务器或 CDN 提供它们,或者将它们包含在你的构建过程中。

运行时生成

你只需在你的布局中添加必要的 <script> 标签。以下是一个 blade 视图示例

@include('js-localization::head')
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Test view</title>
        @yield('js-localization.head')
    </head>
    <body>
        <p>
            Here comes a translated message:
            <script type="text/javascript">
                document.write( Lang.get('reminder.user') );
            </script>
        </p>
    </body>
</html>

请记住,最好不要将 @yield('js-localization.head') 放在 <head> 中,因为它包含发送此包前端部分的 <script> 标签。最佳实践是将它放在 <body> 的末尾,但在其他 <script> 标签之前。上面的示例只是简单地将其包含在 <head> 中,因为这是最简单的使用方式。

静态生成

为了提高性能,你可以生成包含所有生成字符串的静态 JavaScript 文件。这些文件可以直接作为静态文件提供,或者作为前端资产构建过程的一部分包含在内。

要指定资产的输出目录,只需在 config/js-localization.php 文件中相应地设置 $storage_path 字符串(见 配置)。

    /*
    |--------------------------------------------------------------------------
    | Define the target to save the exported messages to
    |--------------------------------------------------------------------------
    |
    | Directory for storing the static files generated when using file storage.
    |
    */

    'storage_path' => public_path('vendor/js-localization/'),

然后可以使用 artisan 命令生成这些文件

php artisan js-localization:export

这将在你目标目录中生成两个文件

  • messages.js 包含你的翻译字符串
  • config.js 包含你导出的配置值

如果你想在 config/js-localization.php 配置文件中将 messages.js 文件自动拆分为每个语言单独的 .js 文件,你可以将以下设置为 true

    'split_export_files' => true,

这将在你的目标目录中生成以下文件

  • lang-{locale}.js 包含一种语言的翻译字符串,如果将 split_export_files 配置选项设置为 true

请记住,每次编辑、添加或删除任何翻译字符串时,都需要使用 php artisan js-localization:export 重新生成文件。

功能

您可以在您的JavaScript代码中使用Lang.get()、Lang.has()、Lang.choice()、Lang.locale()和trans()(Lang.get()的别名)功能。它们与Laravel的Lang外观类似。此外,您还可以将配置属性传递到您的JavaScript代码中。JavaScript中也有Config.get()。使用config/js-localization.php中的config字段配置要传递到客户端的配置属性。注意:不要导出任何安全关键属性,如数据库凭据等,因为这些属性会对使用您的应用程序的任何人可见!

消息中的变量是支持的。例如:"This is my test string for :name."

复数形式也受支持,但不会考虑区域设置。它只使用英语复数规则("singular text|plural text")。更复杂的复数量化词尚不支持。

服务提供商

假设您正在开发一个依赖于此JavaScript本地化功能的Laravel包,并且您想配置哪些包的消息需要对JS代码可见。

幸运的是,这很简单。只需监听JsLocalization.registerMessages事件,并使用JsLocalization\Facades\JsLocalizationHelper::addMessagesToExport()方法。如下所示

<?php

use Illuminate\Support\ServiceProvider;
use JsLocalization\Facades\JsLocalizationHelper;

class MyServiceProvider extends ServiceProvider
{
    /* ... */

    public function register()
    {
        Event::listen('JsLocalization.registerMessages', function()
        {
            JsLocalizationHelper::addMessagesToExport([
                // list the keys of the messages here, similar
                // to the 'messages' array in the config file
            ]);
        });
    }

    /* ... */
}

许可证

本软件采用MIT许可证发布。请参阅许可证