WPML

WPML 允许您通过在 wpml-config.xml 文件中配置设置,自定义语言切换器以满足您的特定需求。

wpml-config.xml 是一个配置文件,允许您自定义 WPML 语言切换器在网站上的外观和行为。您可以配置许多属性,例如 CSS 样式、显示设置和简码。

本指南适用于想要自定义内置 WPML 语言切换器的开发者。如果您想构建自定义语言切换器,请阅读我们关于创建自定义语言切换器的页面。

需要生成 wpml-config.xml 文件?请访问我们的分步指南,了解如何使用 Multilingual Tools 插件自动生成配置文件

可以在语言切换器中自定义哪些内容?

WPML 允许您自定义语言切换器中的许多变量。这包括:

  • 内联 CSS 样式 – 将自定义 CSS 直接添加到语言切换器
  • 指向未翻译内容的链接 – 决定是显示还是隐藏指向未翻译内容的链接
  • 要复制的 URL 参数 – 指定在切换语言时应复制的 URL 参数
  • 侧边栏/页脚语言切换器 – 配置侧边栏和页脚中语言切换器的外观,包括:
    • 是否显示旗帜和/或语言名称
    • 是否将当前语言显示为链接
    • 为语言切换器小工具设置标题
    • 为切换器的布局选择模板
    • 使用十六进制颜色代码自定义文本、背景和边框的颜色
  • 简码操作 – 定义通过简码添加的语言切换器的外观
为内置 WPML 语言切换器添加自定义配置的示例
<wpml-config>
  <language-switcher-settings>
    <key name="additional_css">{inline CSS styles}</key>
    <key name="link_empty">{0 or 1}</key>
    <key name="copy_parameters">{parameter1, parameter2}</key>
    <key name="sidebars">
        <key name="{sidebar slug}">
            <key name="display_flags">{0 or 1}</key>
            <key name="display_names_in_current_lang">{0 or 1}</key>
            <key name="display_names_in_native_lang">{0 or 1}</key>
            <key name="display_link_for_current_lang">{0 or 1}</key>
            <key name="widget_title">{widget title}</key>
            <key name="template">{template slug}</key>
            <!-- color picker keys -->
            <key name="background_normal">#{hex color}</key>
            <key name="border_normal">#{hex color}</key>
            <key name="font_current_normal">#{hex color}</key>
            <key name="font_current_hover">#{hex color}</key>
            <key name="background_current_normal">#{hex color}</key>
            <key name="background_current_hover">#{hex color}</key>
            <key name="font_other_normal">#{hex color}</key>
            <key name="font_other_hover">#{hex color}</key>
            <key name="background_other_normal">#{hex color}</key>
            <key name="background_other_hover">#{hex color}</key>
        </key>
    </key>
    <key name="statics">
        <key name="footer">
            <key name="show">{0 or 1}</key>
            <key name="display_flags">{0 or 1}</key>
            <key name="display_names_in_current_lang">{0 or 1}</key>
            <key name="display_names_in_native_lang">{0 or 1}</key>
            <key name="display_link_for_current_lang">{0 or 1}</key>
            <key name="template">{template slug}</key>
            <!-- color picker keys -->
            <key name="background_normal">#{hex color}</key>
            <key name="border_normal">#{hex color}</key>
            <key name="font_current_normal">#{hex color}</key>
            <key name="font_current_hover">#{hex color}</key>
            <key name="background_current_normal">#{hex color}</key>
            <key name="background_current_hover">#{hex color}</key>
            <key name="font_other_normal">#{hex color}</key>
            <key name="font_other_hover">#{hex color}</key>
            <key name="background_other_normal">#{hex color}</key>
            <key name="background_other_hover">#{hex color}</key>
        </key>
        <key name="post_translations">
            <key name="show">{0 or 1}</key>
            <key name="display_flags">{0 or 1}</key>
            <key name="display_names_in_current_lang">{0 or 1}</key>
            <key name="display_names_in_native_lang">{0 or 1}</key>
            <key name="display_link_for_current_lang">{0 or 1}</key>
            <key name="display_before_content">{0 or 1}</key>
            <key name="display_after_content">{0 or 1}</key>
            <key name="template">{template slug}</key>
            <key name="availability_text">{string like "See post translations: %s"}</key>
        </key>
        <key name="shortcode_actions">
            <key name="display_flags">{0 or 1}</key>
            <key name="display_names_in_current_lang">{0 or 1}</key>
            <key name="display_names_in_native_lang">{0 or 1}</key>
            <key name="display_link_for_current_lang">{0 or 1}</key>
            <key name="template">{template slug}</key>
            <!-- color picker keys -->
            <key name="background_normal">#{hex color}</key>
            <key name="border_normal">#{hex color}</key>
            <key name="font_current_normal">#{hex color}</key>
            <key name="font_current_hover">#{hex color}</key>
            <key name="background_current_normal">#{hex color}</key>
            <key name="background_current_hover">#{hex color}</key>
            <key name="font_other_normal">#{hex color}</key>
            <key name="font_other_hover">#{hex color}</key>
            <key name="background_other_normal">#{hex color}</key>
            <key name="background_other_hover">#{hex color}</key>
        </key>
    </key>
  </language-switcher-settings>
</wpml-config>

并非所有这些部分都必须存在于您的配置文件中。只需包含适用于您的插件或主题的部分。

将 WPML 语言切换器重置为默认设置

如果您在后端更改了语言切换器的配置(不同于初始值),您也可以使用 wpml-config.xml 来重置该配置。
要查看新的更改,请确保单击 WPML 语言页面底部的恢复默认设置按钮。

其他资源

要了解有关 wpml-config.xml 文件的更多信息,请访问语言配置文件指南。

要使用 wpml-config.xml 文件自定义其他元素,请访问我们的其他指南:

作者:Amir · 最后更新时间:2024年1月5日