rnr1721/multilingual-laravel 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

rnr1721/multilingual-laravel

最新稳定版本:1.0.0

Composer 安装命令:

composer require rnr1721/multilingual-laravel

包简介

Laravel implementation for multilingual support (language switcher and URL prefixes)

README 文档

README

Laravel Multilingual Support Package (Languge switcher)

This package provides multilingual support for Laravel applications, allowing easy management of multiple languages, automatic URL prefixing, language switching and locale handling.

Looking for a robust multilingual solution for your Laravel project? This package provides a clean, efficient way to handle multiple languages in your web applications with a focus on developer experience and SEO-friendly URLs.

What makes it special? Unlike traditional approaches, it separates core multilingual logic from framework implementation, making it more maintainable and portable. You get automatic language detection from URLs, smart prefix handling, and seamless locale switching - all while maintaining Laravel's elegant syntax and routing flexibility.

The package is production-ready with features like RTL support, automatic SEO-friendly URL generation, and comprehensive Blade integration. It's thoroughly tested and follows Laravel best practices, making it a reliable choice for projects of any size - from small corporate websites to large international platforms.

Integration is a breeze: just install the package, define your languages in config, and use the intuitive multilingual() route macro. The package handles all the complexities of language switching and URL generation while keeping your code clean and maintainable. No database queries, minimal overhead, and maximum flexibility - just the way Laravel developers like it.

Requirements:

  • PHP 8.1 or higher
  • Laravel 10.0 or higher
  • rnr1721/multilingual-core: ^1.0 (installed automatically as requirement)

Installation in project

composer require rnr1721/multilingual-laravel

Features:

  • Automatic language detection from URL
  • Language prefix management in URLs
  • Blade directives for language switching
  • Route macro for multilingual routes
  • Automatic framework locale switching
  • View variables for language data
  • RTL language support

Middleware: No need to manually manage language detection; LaravelLanguageMiddleware automatically switches the current language based on the URL.

Configuration:

Publish the configuration file:

php artisan vendor:publish --provider="rnr1721\MultilingualLaravel\LaravelMultilingualServiceProvider" --tag="config"

Configure your languages in config/multilingual.php:

<?php

return [
    'languages' => [
        'en' => [
            'name' => 'English',
            'locale' => 'en_US',
            'rtl' => false
        ],
        'es' => [
            'name' => 'Español',
            'locale' => 'es_ES',
            'rtl' => false
        ]
    ],
    'default_language' => 'en',
    'url' => [
        'exclude_default_language' => true
    ],
    'preload_page_links_in_templates' => true
];

Usage:

  1. Define multilingual routes:
Route::multilingual(function () {
    Route::get('/', function () {
        return view('welcome');
    })->name('welcome');
    
    Route::get('/about', function () {
        return view('about');
    })->name('about');
});

This will create routes for all configured languages:

  • / and /about for English (default)
  • /es and /es/about for Spanish
  1. Use in templates:
<nav>
    <ul>
        @foreach($pageLinks as $pageLink)
            <li>
                <a href="{{ $pageLink['url'] }}"
                   @if($pageLink['active']) class="active" @endif>
                    {{ $pageLink['name'] }}
                </a>
            </li>
        @endforeach
    </ul>
</nav>
  1. Generate language-specific URLs:
@language('route.name')
// or
@language('route.name', ['param' => 'value'])
// or
@language('route.name', [], 'es')
  1. Available helper functions:
  • language_route($name, $parameters = [], $language = null)
  • language_url($path, $parameters = [], $language = null)
  • current_language()
  • available_languages()
  • page_links()
  1. Available view variables:
  • $currentLanguage - Current language object
  • $languages - Array of all available languages
  • $pageLinks - Array of language URLs for current page
  1. Translations:

Place your translation files in resources/lang/{locale}/ directories matching the locales in your config:

resources/ └── lang/ ├── en_US/ │ └── messages.php └── es_ES/ └── messages.php

Use translations in templates:

{{ __('messages.welcome') }}

Testing:

composer test

Code Style:

composer cs-check
composer cs-fix

Static Analysis:

composer phpstan

License:

MIT

Credits:

Eugeny G rnr1721@gmail.com

For more detailed documentation and examples, please visit the GitHub repository.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-15