MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:akaunting/laravel-language

项目背景与痛点

Laravel 的广泛应用和社区的活跃度,使得它的多语言支持成为开发者们关注的重点。akaunting/laravel-language 解决了一个痛点:为 Laravel 提供强大的语言切换功能。这意味着我们可以轻松地为项目添加多种语言支持,从而确保应用在不同地区和语言环境中都能顺畅地运行。它的存在简化了运维工作,提高了用户体验,让开发者能够更加专注于核心业务逻辑而非大量的重复性翻译工作。

核心技术揭秘

akaunting/laravel-language 包的实现,基于 Laravel 的服务容器和事件驱动机制,巧妙地通过事件监听和中间件机制来自动处理用户的语言偏好设置。它的主要组件包括一个语言切换器和语言相关的一系列事件处理器。

以下是一个简化但精准的技术架构图,展示了其实现的核心流程:

graph TD A[用户请求] --> B{"检测语言偏好"} B -->|"已设置"| C[使用偏好语言] B -->|"未设置"| D[使用默认语言] C --> E[执行视图加载] D --> E[执行视图加载] E --> F[返回响应]

具体而言,该包会在开始处理请求时监听相关事件或中间件,来评估用户的语言偏好,并根据这些偏好动态加载语言文件。由于 Laravel 提供的强大的依赖注入和事件系统,因此在不影响性能的情况下实现这一功能变得更加容易。

功能亮点与差异

对比其他语言切换包,akaunting/laravel-language 的独特之处在于它的灵活性和易维护性。它不仅能够方便地整合各种翻译服务或本地翻译文件,还支持动态的前后端语言检测和切换。特别是对复杂的国际化应用,它可以自适应地根据用户的设备、浏览器偏好设置等来智能切换语言,从而提供了无缝的语言切换体验。

应用场景与落地建议

在使用 akaunting/laravel-language 之前,开发者需确保团队对 Laravel 的事件监听和中间件机制有足够的了解,因为定制化需求往往需要深入理解和灵活利用这些框架特性。该包适用于处理大量国际化用户需求的项目,特别适合那些需要根据用户行为动态调整语言的场景,例如内容丰富的电子商务应用或大型社区网站。

综合评价

akaunting/laravel-language 提供了简洁而强大的语言切换功能,极大地减轻了开发者的本地化工作负担。它的灵活性和可扩展性使其成为一个值得推荐的解决方案。当然,该包的成功应用需要开发者对 Laravel 有深度的理解,并正确地配置和调整来适应具体的应用场景。虽然配置上可能会有一定的学习成本,但整体而言,它的设计思路和技术实现无疑为 Laravel 项目的国际化应用带来了极大的便利性。

关于

GitHub 原始描述(英文)

Language switcher package for Laravel

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
339
价值分
150
活跃状态
活跃

语言占比

Blade
PHP

Release

README

Language switcher package for Laravel

Downloads Tests StyleCI Quality License

This package allows switching locale easily on Laravel projects. It’s so simple to use, once it’s installed, your App locale will change only by passing routes into SetLanguage middleware.

Version Compatibility

Laravel PHP Package
5.8 7.2 - 7.4 ^2.0
6.x 7.2 - 8.0 ^2.0
7.x 7.2 - 8.0 ^2.0
8.x 7.3 - 8.1 ^2.0
9.x 8.0 - 8.2 ^2.0
10.x 8.1 - 8.3 ^2.0
11.x 8.2 - 8.4 ^2.0
12.x 8.2 - 8.5 ^2.0

Top features:

  • Change automatically app locale depending on user browser configuration
  • Language flags built-in for easy implementation
  • Get language name like ‘English’ or ‘Español’ from codes such as ‘en’ or ‘es’
  • Option to choose short (en) or long (en-GB) language code
  • Store locale on users table
  • Restrict users to set languages you don’t have translations
  • Helper functions for clean, simple and easy to read API
  • Supports Carbon and Date packages

Getting Started

1. Install

Run the following command:

composer require akaunting/laravel-language

2. Register (Optional - Laravel 5.5+ Auto-Discovery)

Note: Laravel 5.5+ automatically discovers the package. Manual registration is only needed for older versions.

For Laravel < 5.5, register the service provider in config/app.php:

Akaunting\Language\Provider::class,

Add alias if you want to use the facade:

'Language' => Akaunting\Language\Facade::class,

3. Publish

Publish config, migration and blade files.

php artisan vendor:publish --tag=language

Optional: Publish flag images for customization (only if you want to replace flag images):

php artisan vendor:publish --tag=language-flags

This will copy flag images to public/vendor/language/flags/ where you can replace them with your custom flags. The package will automatically use your custom flags if they exist, otherwise it will use the default package flags.

4. Migrate

Add locale column to users table:

php artisan migrate

5. Configure

Default values can be modified also on config/language.php

Keys
  • route: Makes route available
  • carbon: Sets briannesbitt/carbon translator language
  • date: Sets jenssegers/date translator language
  • home: Make home route available
  • auto: Sets language automatically depending on user’s browser config
  • prefix: Prefix of routes URI to set locale
  • middleware: default middleware to set locale
  • controller: default controller to handle locale
  • flags: Settings such as width, class etc for flags
  • mode: The language code and name mode
  • allowed: Allowed language codes
  • all: Available language names and codes

Usage

Middleware

All routes in which you want to set language should be under the language middleware to set at each request to App locale.

Route::group(['middleware' => 'language'], function () {

    // Here your routes

});

URL

  • Via URL with return home: /languages/{locale}/home
  • Via URL with return back: /languages/{locale}/back

Tip: /languages prefix can be changed from config/language.php

Methods

language()->allowed()

Returns an array with [$code => $name] for all allowed languages of config. Example usage on blade:

@foreach (language()->allowed() as $code => $name)
    <a href="{{ language()->back($code) }}">{{ $name }}</a>
@endforeach

language()->flags()

Returns an output with flags for all allowed languages of config. Output can be changed from resources/views/vendor/language folder

language()->flag()

Returns the flag of the current locale. Output can be changed from resources/views/vendor/language folder

language()->names($codes = null)

Get an array like [$code => $name] from an array of only $codes.

language()->codes($langs = null)

Get an array like [$name => $code] from an array of only $langs.

language()->back($code)

Returns the URL to set up language and return back: back()

Also if you prefer to use directly route() function you can use it as following code:

{{ route('language::back', ['locale' => $code]) }}

language()->home($code)

Returns the URL to set language and return to home: url('/')

Also if you prefer to use directly route() function you can use it as following code:

{{ route('language::home', ['locale' => $code]) }}

language()->getName($code = ‘default’)

Returns the language name of $code if specified or the current language set if not.

Tip: Use app()->getLocale() to get the current locale

language()->getCode($name = ‘default’)

Returns the language code of $name if specified or the current language set if not.

Changelog

Please see Releases for more information what has changed recently.

Contributing

Pull requests are more than welcome. You must follow the PSR coding standards.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

评论

暂无评论