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-defaulttag 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.
- Language Code (e.g. 'EN')
- Long Language Code (e.g. 'EN-US')
- Language Name (e.g. 'English')
- Long Language Name (e.g. 'English (United States)')
- 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:
keepQueryParameters→true,falseshowLanguageInThatLanguage→true,falselabelType→code,code-long,name,name-long,custom- They represent each of the options explained here
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spacemonk/craft-language-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Puts the Symfony Translation Component on steroids
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
统计信息
- 总下载量: 399
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2022-11-14