定制 hadefication/polyglot 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hadefication/polyglot

Composer 安装命令:

composer require hadefication/polyglot

包简介

Extend Laravel Translation to JavaScript.

README 文档

README

Polyglot creates a blade directive @polyglot that you can add to your master blade file. This will then export a global variable Polyglot where all of your app's current locale translation keys are stored. The root object keys are base from the defined translation files located in the Polyglot's configuration file.

Installation

  1. composer require hadefication/polyglot
  2. Add Hadefication\Polyglot\PolyglotServiceProvider::class to your config/app.php under the providers array. This step is not needed for version 5.5 and above where package auto-discovery is introduced.
  3. Include @polyglot blade directive to your master blade file on top of your JavaScript files -- probably in the header or before the body tag ends.

Usage

Once installed, this package will then expose a Polyglot variable where all of your current locale translation keys are stored.

A nifty JavaScript helper function will be exposed too that you can use to translate translation keys like what we're doing in Laravel. Accidentally named it trans too. See examples below for more details on trans helper function.

Example

Without param

trans('auth.failed');

// Should return the equivalent translation of the supplied key

Translations with params

trans('validation.required', {attribute: 'email'});

// Should return the equivalent translation of the supplied key including the supplied params

Config

A configuration file is included too to customize the translation files that will be loaded to Polyglot. To publish the included config file, run php artisan vendor:publish.

Artisan Command

An artisan command is also provided where it will dump a JavaScript file that houses all collected translation keys including the importable route method helper. Upon using this approach, including the @polyglot blade directive won't be necessary.

php artisan polyglot:dump

The command above should dump a JavaScript file named polyglot.js in your /resources/assets/js directory. You can also supply --path=/path/to/where/the/dump/file/will/be/exported to dumpt the file in other location. The command should look like php artisan polyglot:dump --path=./resources/assets/js/vendor/polyglot.js.

// ES6
import './path/to/polyglot';

// Old School
require('./path/to/polyglot.js');

The code above should be added to your bootstrap file or to the main JavaScript file if you have a custom entry point.

Laravel Mix

You can also automate the dumping by installing a webpack plugin that runs a simple artisan command on every build so you are sure that you got the latest translation files included in your build. Follow steps below:

  1. Install the webpack shell plugin: npm install --save-dev webpack-shell-plugin or yarn add --dev webpack-shell-plugin
  2. Include the plugin to your webpack.mix.js file:
const mix = require('laravel-mix');
const WebpackShellPlugin = require('webpack-shell-plugin');

mix.webpackConfig({
    plugins: [
        new WebpackShellPlugin({ onBuildStart: ['php artisan polyglot:dump'], onBuildEnd: [] }),
    ]
});

....
  1. Done! This will run php artisan polyglot:dump on start of the build so you get the latest translation files.

hadefication/polyglot 适用场景与选型建议

hadefication/polyglot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hadefication/polyglot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hadefication/polyglot 我们能提供哪些服务?
定制开发 / 二次开发

基于 hadefication/polyglot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 66
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-08-15