spacemonk/craft-language-manager 问题修复 & 功能扩展

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

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

spacemonk/craft-language-manager

Composer 安装命令:

composer require spacemonk/craft-language-manager

包简介

Add language navigation and valid hreflang header attributes for multi language sites

README 文档

README

The Language Manager plugin adds easy integration for a language switcher and for the correct hreflang attributes in the header.

  • The language switcher is a simple menu that links to all parallel language versions of the same page
  • The hreflang tags add the correct hreflang attributes in the header of the page (including setting the x-default tag for the primary version)

Requirements

This plugin requires Craft CMS 4.0 or later.

Installation

You can install this plugin from the plugin Store or with Composer.

From the Plugin Store

Go to the Plugin Store in your project’s Control Panel and search for “Language Manager”. Then press Install in its modal window.

With Composer

Open your terminal and run the following commands:

# go to the project directory
cd /path/to/my-project

# tell Composer to load the plugin
composer require spacemonk/language-manager

# tell Craft to install the plugin
php craft plugin/install language-manager

Language Switcher

The sites have to be added in the same site group in Craft CMS.

The language switcher will switch to the parallel site in a different language, if there is no parallel site, it will fall back to the main page. The labels can be set to different styles or you can provide your own. The plugin supports keeping query parameters for the url (default off)

Easy → via hook

In your twig template at the position where you want to have the language switcher menu, add the following hook:

{% hook 'languageManagerNavigation' %}

You can also use caching (be careful when using cache together with the activated setting to keep query parameters).

{% cache %}
    {% hook 'languageManagerNavigation' %}
{% endcache %}

The main nav has the class language-manager for easy CSS targeting.

The output will look like this:

<nav aria-label="Change language" class="language-manager">
  <ul>
    <li class="active">
      <a href="https://yourwebsite.domain/" hreflang="en" lang="en">
        English
      </a>
    </li>
    <li>
      <a href="https://yourwebsite.domain/de/" hreflang="de" lang="de">
        German
      </a>
    </li>
  </ul>
</nav>

Advanced → custom integration

The plugin exposes a twig function getLanguageManagerPages(), which returns an array of page objects. For details see below.

As a starting point you can copy the code from the hook template here.

Labels

You can choose from several options to display the language switcher labels.

  1. Language Code (e.g. 'EN')
  2. Long Language Code (e.g. 'EN-US')
  3. Language Name (e.g. 'English')
  4. Long Language Name (e.g. 'English (United States)')
  5. Your own custom label translations, see here

Hreflang Tags

Easy → via hook

In your template (in the html head), add the following hook:

{% hook 'languageManagerHreflang' %}

You can also use caching.

{% cache %}
    {% hook 'languageManagerHreflang' %}
{% endcache %}

The output will look like this:

<link rel="alternate" hreflang="en" href="https://yourwebsite.domain/" />
<link rel="alternate" hreflang="x-default" href="https://yourwebsite.domain/" />
<link rel="alternate" hreflang="de" href="https://yourwebsite.domain/de/" />

The x-default attribute is set to the primary site.

Advanced → custom integration

The plugin exposes a twig function getLanguageManagerPages(), which returns an array of page objects. For details see below.

As a starting point you can copy the code from the hook template here.

Custom Label Translations

If you choose to provide your own label names, you can do so via the translations. The plugin uses the language codes (e.g. en or en-US) as translation key for the labels. To add your labels create a translation file with the name language-manager.php and place it in the folder with the specified language (e.g. yourprojectroot/translations/en/language-manager.php. Then add your translations:

<?php

return [
    'en' => 'English',
    'en-US' => 'American',
    'de' => 'German',
    'de-CH' => 'Swiss German',
];

Twig function

The plugin exposes a twig function getLanguageManagerPages(), which returns an array of page objects. Each page object has the following fields:

'url' => The url
'queryParameters' => The query parameters to concatenate with the url (always empty if setting 'keepQueryParameters' == false)
'label' => The label to show in the navigation
'isoCountryCode' => The ISO country code (e.g. 'EN', 'DE' or 'CH' (ISO 3166-1 Alpha-2 code))
'isoLanguageCode' => The ISO language code (e.g. 'en', 'de' or 'de-CH' (ISO 639-1 and two letter country code where relevant))
'isActive' => true if this is the current active language version, else false
'isPrimarySite' => true if this language version is the primary site in the control panel
'isFallback' => true if no language version exists (this means the url points to the base path)

Settings

Control Panel

Settings can be easily adjusted in the control panel.

Config

Settings can also be set via config and will overwrite and disable the Control Panel settings.

Create a file called language-manager.php and place it in the config folder (e.g. yourprojectroot/config/language-manager.php). Then add your config:

<?php

return [
    'keepQueryParameters' => false,
    'showLanguageInThatLanguage' => false,
    'labelType' => 'code'
];

Following options are possible:

  • keepQueryParameterstrue, false
  • showLanguageInThatLanguagetrue, false
  • labelTypecode, code-long, name, name-long, custom

spacemonk/craft-language-manager 适用场景与选型建议

spacemonk/craft-language-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 399 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 11 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 spacemonk/craft-language-manager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2022-11-14