0

我正在编写一个 404 自定义页面,一切都很好,但我正在使用 WPML,因此我正在本地化我的字符串。

这是我的代码的一部分:

<h4><?php _e('You still can\'t find anything?', 'kslang'); ?></h4>
<h5><?php _e('Well, then you probably should give us a wake up call...', 'kslang'); "\n"?></h5>
<h5><?php _e('but be aware who you\'re waking up!', 'kslang');?></h5>
<h5><?php _e('You\'re sure? Well, then...', 'kslang'); ?></h5>
<form>
    <input type="button" value="<?php _e('Contact Us!', 'kslang'); ?>" onClick="window.location.href='http://example.com/contact-us-today'">
</form>
<h5><?php _e('or try the Site Map Below. You\'re also welcome to check out our related projects!', 'kslang'); ?></h5>

现在的问题是,我的

"window.location.href='http://example.com/contact-us-today'"

没有重定向到相关语言。

我虽然可以简单地将 href 链接包装在获取文本中,例如通过按钮文本和其余字符串。

这不行,显然事情要复杂得多。我需要使用 if/else 语句吗?

有人知道如何重定向到正确的语言吗?(我尝试使用 _e 来翻译字符串翻译器中的 href 链接,但这不适用于 _e,因为在这种情况下插入 php 片段会破坏我的网站)

希望有人可以提供意见...

4

1 回答 1

0

要使用 window.location.href 重定向到适当的语言版本,您需要在 URL 中包含该语言...即,对于法语,重定向到“ http://www.example.com/fr/contact-us-今天”(在域名后面加上“fr”)。您可以通过检测 PHP 中的当前 WPML 语言并将其作为 JavaScript 变量回显来动态执行此操作,如下所示:

//Get current WPML language
global $sitepress;
$language = $sitepress->get_current_language();
//Echo as JS variable
echo "<script>var lang = '".$language."';</script>"

然后,您可以像这样重定向:

window.location.href='http://www.example.com/'+lang+'/contact-us-today';

注意:此示例假设您使用不同语言的语言文件夹。如果您使用子域或语言查询参数,则需要进行相应调整。

于 2015-01-22T11:34:02.087 回答