我正在构建一个支持英语和丹麦语两种语言的 WordPress 网站。
我想保留语言代码字符串en
对于英语和da
对于请求 uri 中前置的丹麦语。
就像:(目前这对我有用)
http://example.com/da
如果我访问帖子或页面,它应该是这样的地图:(这不起作用,得到 404)
http://example.com/da/post-name
http://example.com/da/page-name
http://example.com/da/post/is/too/long
我也尝试过WordPress 重写 API http://codex.wordpress.org/Rewrite_API
add_rewrite_rule() http://codex.wordpress.org/Rewrite_API/add_rewrite_rule(重写目前我拥有的规则)
<?php
add_action('init', function () {
add_rewrite_rule(
'^(da|en)/?', //Regex
'index.php?lang=$matches[1]', //request to
'top' //called earlier than wordpress rules
);
});
并且add_rewrite_tag() http://codex.wordpress.org/Rewrite_API/add_rewrite_tag,但我认为Wordpress只是提供了一个add_rewrite_endpoint http://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint(而且我根本不需要这个)。
我认为这可能只能通过 htaccess 实现%{QUERY_STRING}
状况? (不知道)
.htaccess 内容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Edit:
我在用着WP 本机仪表板 http://wordpress.org/plugins/wp-native-dashboard/用于管理页面上的翻译,但是在前面我只是使用__()
and _e()
with .mo
and .po
文件及其工作完美。
P.S:
这个问题并不是 WordPress 网站特有的,我将来也需要基于自定义网站的帮助。如果可以的话,请向我提供 .htaccess 规则/条件。