承接 zachleigh/laravel-lang-bundler 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

zachleigh/laravel-lang-bundler

最新稳定版本:v1.1.0

Composer 安装命令:

composer create-project zachleigh/laravel-lang-bundler

包简介

Create Laravel translations bundles.

README 文档

README

Latest Stable Version License Build Status Quality Score StyleCI Total Downloads

Make bundles of translation values.

Contents

Why

Why use this package? Because this sucks:

<component
    welcome={{ trans('home.welcome') }}
    login={{ trans('home.login') }}
    signup={{ trans('home.signup') }}
    profile={{ trans('user.settings.profile') }}
    friends={{ trans('user.home.friends') }}
    intro={{ trans('user.home.intro') }}
    body={{ trans('user.home.body') }}
></component>

And this is so much better:

<component
    :lang={{ transB('bundle_name')->toJson() }}
></component>

Upgrade Information

Version 1.0.* to Version 1.1.*

Version 1.1.0 drops support for PHP versions lower than 7.1.

Version 0.9.* to Version 1.0.0

Version 1.0.0 is more a confimation of the current api and usage than anything else. Adds support for Laravel 5.4. If using Laravel 5.3, please use Version 0.9.11:

composer require zachleigh/laravel-lang-bundler:0.9.*

Install

1. Install through composer
composer require zachleigh/laravel-lang-bundler
2. Register the service provider

In Laravel's config/app.php file, add the service provider to the array with the 'providers' key.

LaravelLangBundler\ServiceProvider::class
3. Publish the config file:
php artisan vendor:publish --tag=config
4. Create a 'bundles' directory in resources/lang/.

Do it manually or use the command:

php artisan langb:start

Usage

1. Make a bundle.

Imagine we have two lang files, one called 'home.php' and one called 'user.php'.

home.php

    'welcome' => 'Welcome!',
    'login' => 'Login',
    'signup' => 'Signup',

user.php

    'profile' => 'Your Profile',
    'friends' => 'Your Friends',
    'body' => 'Enter body below',

We want all of these values in one bundle.

Simply register your bundle as an array anywhere in the bundles directory. For example, in the bundles directory, you could create a folder called 'components' and in it a file called 'bundle_name' that looks like this:

return [
    'home.welcome',
    'home.login',
    'home.signup',
    'user.profile',
    'user.friends',
    'user.body'
];

Obviously, 'bundle_name' is the name of the bundle. The other values represent keys found in the above lang files.

Like in other lang folders, any file/folder in the bundles directory is treated as a level in an array. So in the above example, our file path looks like this:

lang/bundles/components/bundle_name.php

The path for the 'bundle_name' bundle would be 'bundles.components.bundle_name'. It is also possible to create multiple named bundles within a single file, but this is not recommended because you can not use auto-aliasing for multi-bundle files.

2. Get the bundle using the transB() helper function.

Get your translated bundle by passing the bundle path to the transB() helper function.

transB('bundles.components.bundle_name');

Or use the auto-aliased name:

transB('bundle_name');

transB() returns a collection of translated values keyed by the original translation key. Continuing the example above, transB() would give us a collection that contains the following array:

[
    'welcome' => 'Welcome!',
    'login' => 'Login',
    'signup' => 'Signup',
    'profile' => 'Your Profile',
    'friends' => 'Your Friends',
    'body' => 'Enter body below',
];
3. Pass parameters to your bundle.

Like with the standard trans() function, you may pass parameters to the transB() function as the second argument.

transB('bundle_name', ['parameterName' => $value]);

If your bundle has conflicting parameter names, you can namespace them. In this example, three values require a user parameter.

user.php translation file:

return [
    'welcome_user' => 'Welcome :user',
    'message_to'   => 'You sent a message to :user',
    'invite_from'  => 'You have an invite from :user'
];

Bundle file:

return [
    'user.welcome_user',
    'user.message_to',
    'user.invite_from'
];

Avoid the naming conflict by namespacing the parameters when passing them to transB():

transB('bundle_name', [
    'welcome_user.user' => 'Bob',
    'message_to.user' => 'Sally',
    'invite_from.user' => 'George'
]);
4. Pluralize values

It is possible to pluralize lang items by passing a namespaced 'choice' parameter in the transB() function parameters. For example, if our lang file value looked like this:

'inbox_status' => 'You have a new message.|You have new messages'

We could register it in our bundle normally:

'home.inbox_status'

And then when calling transB(), pass a parameter called 'inbox_status.choice' with the desired choice value:

transB('bundle_name', ['inbox_status.choice' => 3]);

The result will look be the pluralized string "You have new messages".

Advanced Usage

Modify return keys and values

To modify the key and value in the returned translation array, use the bundle_item() helper on a specific bundle item.

bundle_item($id, $type, $parameters = []);

$id is the lang key. $type must be in the following format: 'target_type'. 'target' declares what item is to be affected by the modification and can be set to either 'value', 'key', or 'both'. 'type' declares the type of modification (callback, change etc.). $parameters is an array of parameters to be sent to the class that performs the modification.

If using the same example as above we wanted to convert the 'welcome_user' value to all caps, we could accomplish it by using the bundle_item() helper function in the bundle file.
user.php translation file:

return [
    'welcome_user' => 'Welcome :user',
    'message_to'   => 'You sent a message to :user',
    'invite_from'  => 'You have an invite from :user'
];

Bundle file:

return [
    bundle_item('user.welcome_user', 'value_strtoupper'),
    'user.message_to',
    'user.invite_from'
];

Wrap the bundle key 'user.welcome_user' in the bundle_item() global function and pass the translation key ($id) plus the type (perform a 'strtoupper' on the returned 'value'). This returns the following values (assuming a non-namespaced user variable with the value 'Bob'):

[
    'welcome_user' => 'WELCOME BOB',
    'message_to'   => 'You sent a message to Bob',
    'invite_from'  => 'You have an invite from Bob'
];

If we wanted to do the same to the key, we could do this:

return [
    bundle_item('user.welcome_user', 'key_strtoupper'),
    'user.message_to',
    'user.invite_from'
];

Or, if we wanted to perform the modification on both the key and the value:

return [
    bundle_item('user.welcome_user', 'both_strtoupper'),
    'user.message_to',
    'user.invite_from'
];
Available modifiers
callback

Perform a callback on a key or value. Requires a 'callback' parameter.

bundle_item('user.welcome_user', 'value_callback', [
    'callback' => 'function_name'
]),
change

Change a key to a new value. Does nothing to values. Requires 'new' parameter.

bundle_item('user.invite_from', 'key_change', [
    'new' => 'newKey'
]),
explode

Explode by given delimiter. Does nothing to key. Requires 'delimiter' parameter.

bundle_item('user.invite_from', 'value_explode', [
    'delimiter' => ' '
]),
strtolower

Lowercase entrire string.

bundle_item('home.invite_from', 'value_strtolower')
strtoupper

Capitalize entire string.

bundle_item('home.invite_from', 'value_strtoupper')
ucfirst

Make first character in string capitalized.

bundle_item('home.invite_from', 'value_ucfirst')
values

If translation value is an array, run array_values() on array and return only values keyed by integers. Does nothing to keys.

bundle_item('home.months', 'value_values')
Creating your own modifier

Use the 'mod' command to create a new mod class in App/LangBundler/Mods:

langb:mod {name}

There are two abstract methods that must be implemented in your class:

    /**
     * Alter key and return.
     *
     * @param string $key
     *
     * @return string
     */
    abstract public function key($key);

    /**
     * Alter value and return.
     *
     * @param mixed $value
     *
     * @return mixed
     */
    abstract public function value($value);

The same class is used to modify both the value and key. Define your modification and return the desired key/value.

Commands

php artisan langb:start

Get started by creating a bundles directory in your lang folder.

php artisan langb:new {path}

Create a new bundle file located at path. For example:

php artisan langb:new components.user.profile

This would create the file lang/bundles/components/user/profile.php with an empty returned array.

php artisan langb:mod {name}

Create an empty mod template in App/LangBundler/Mods.

Configuration

aliases

To shorten the name of bundles, you can register aliases in config.

'aliases' [
    'alias' => 'full.path.to.bundle'
];

And then simply use the alias istead of the path in transB():

transB('alias');
key_transform

If you wish to transform lang file keys to snake_case, StudlyCase, or camelCase, set key_transform to 'snake_case', 'studly_case', or 'camel_case'. Default value is 'none'.
For example, this bundle contains snake cased variables:

return [
    'user.welcome_user',
    'user.message_to',
    'user.invite_from'
];

But in your javascript, you want to use came cased variables, set key_transform to 'camel_case' to get this bundle:

return [
    'welcomeUser' => 'Welcome user!',
    'messageTo' => 'Message to user',
    'inviteFrom' => 'You have an invitation from user!',
];

Many other simple string functions (ucfirst, strtoupper, etc.) also work.

key_transform is global and will transform all keys in your project. If you wish to transform a single key, see modify return keys and values.

global_key_namespace

If you keep all your translations in a single file, you can set global_key_namespace to the name of the file to save yourself some typing. For example, if all your translations are in a file called 'translations.php', you would have to register a bundle like this:

return [
    'bundle_name' => [
        'translations.home',
        'translations.navigation',
        'translations.menu',
        'translations.login'
    ];
];

However, if you set global_key_namespace to 'translations', you could register it like this:

return [
    'bundle_name' => [
        'home',
        'navigation',
        'menu',
        'login'
    ];
];

Testing

composer test

Contributing

Contributions are more than welcome. Fork, improve and make a pull request. For bugs, ideas for improvement or other, please create an issue.

zachleigh/laravel-lang-bundler 适用场景与选型建议

zachleigh/laravel-lang-bundler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.47k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2016 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translation」 「language」 「laravel」 「localization」 「lang」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 zachleigh/laravel-lang-bundler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 12.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-30