承接 unique/yii2-translatable-url-rule 相关项目开发

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

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

unique/yii2-translatable-url-rule

Composer 安装命令:

composer require unique/yii2-translatable-url-rule

包简介

A custom URL rule class for Yii 2 which allows to create translated URL rules

README 文档

README

Latest Stable Version Total Downloads License

Yii2 TranslatableUrlRule

A custom URL rule class for Yii 2 which allows for translated URL rules.

This extension allows you to write URL rules per language. For example, you can have signup for en, aanmelden for nl and registrieren for de. Because this extension uses normal Yii2 UrlRule objects to build language-specific URL rules, you still have all power that comes with normal Yii URL rules, including URL parameters and regular expressions.

The current language is determined by the Yii::$app->language parameter. The value of this parameter is used to create and to parse URLs.

This extension does not set the current language parameter. Use codemix/yii2-localeurls for that.

Installation

The preferred way to install this extension is through composer.

Either run

composer require geertw/yii2-translatable-url-rule

or add

"geertw/yii2-translatable-url-rule": "*"

to the require section of your composer.json file.

Usage

Update your URL rules to use this class and set the patterns per language.

<?
$rules = [
    [
        'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
        'patterns' => [
            'en' => '/signup',
            'nl' => '/aanmelden',
            'de' => '/registrieren',
        ],
        'route' => 'user/signup',
    ],
    [
        'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule',
        'patterns' => [
            'en' => '/user/<username>',
            'nl' => '/gebruiker/<username>',
            'de' => '/benutzer/<username>',
        ],
        'route' => 'user/view',
    ],
];
?>

The route parameter remains the same for all rules. patterns is an array of all patterns, the key must equal the Yii language identifier. If there is no pattern for a language, the first configured pattern will be used.

Set forceRuleSearch to true to force searching in all rule patterns.

You may omit the class configuration in your URL rules when you configure a ruleConfig in UrlManager:

<?
return [
    'components' => [
        'urlManager'   => [
            'ruleConfig' => [
                'class' => 'geertw\Yii2\TranslatableUrlRule\TranslatableUrlRule'
            ],
            'rules' => $rules,
            // Additional UrlManager configuration
        ],
    ];
?>

Language switcher example

Due to the way this library works, you need to specify two language parameters when creating URLs for routes in another language.

The following two code snippets allow you to create a simple dropdown which allows users to select alternative languages for the current route:

Create a widget like this:

<?php

namespace frontend\components;

use Yii;
use yii\bootstrap\Dropdown;

class LanguageSwitcher extends Dropdown {
    public $langLabels;

    private $isError;

    public function init() {
        $route = Yii::$app->controller->route;
        $params = $_GET;
        $this->isError = $route === Yii::$app->errorHandler->errorAction;

        array_unshift($params, '/' . $route);

        foreach (Yii::$app->urlManager->languages as $language) {
            $isWildcard = substr($language, -2) === '-*';
            if ($isWildcard) {
                $language = substr($language, 0, 2);
            }
            $params['language'] = $language;
            $params['url-language'] = $language;
            $this->items[] = [
                'label' => $this->label($language),
                'url'   => $params,
            ];
        }
        parent::init();
    }

    public function run() {
        // Only show this widget if we're not on the error page
        if ($this->isError) {
            return '';
        } else {
            return parent::run();
        }
    }

    public function label($code) {
        return isset($this->langLabels[$code]) ? $this->langLabels[$code] : null;
    }
}

Insert the following code in your view code:

    <?= LanguageSwitcher::widget([
        'options'    => ['class' => 'pull-right'],
        'langLabels' => [
            'de' => 'German',
            'en' => 'English',
            'nl' => 'Nederlands',
        ],
    ]) ?>

You might recognize this example, as it is largely based on the example for yii2-localeurls.

unique/yii2-translatable-url-rule 适用场景与选型建议

unique/yii2-translatable-url-rule 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 03 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 unique/yii2-translatable-url-rule 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-07