如何使用 jQuery 根据站点语言有条件地显示 link
How to display link conditionally based on the site's language using jQuery
我正在尝试使用 WPML 在 wordpress 的 .JS 文件中有条件地显示 link。我已经尝试了以下但现在正在工作;
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
if (getLangCode === 'en-US') {
var imagesPath = 'https://website-domain.com/file-name.jpg';
}else if (getLangCode === 'fr-FR') {
var imagesPath = 'https://website-domain.com/fr/file-name.jpg';
}
我在上面的行中收到以下错误 Uncaught SyntaxError: Unexpected token <
我正在使用 link 将图像和文本添加到容器中...文本工作正常,因为我使用 wp_localize_script
使字符串可翻译但是当我切换到法语,图像不再显示,因为 link 现在包含 fr
如能帮助解决此问题,我们将不胜感激
尝试:
1) 删除这一行:
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
2) 将此代码添加到您的 functions.php
add_action('wp_head', 'change_this_name');
function change_this_name() {
?>
<script type="text/javascript">
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
</script>
<?php
};
由于您已经在使用 wp_localize_script,我将使用它不仅传递翻译,还传递您的语言代码,如某些评论和教程中所述。像这样:
在 WordPress 中
wp_enqueue_script( 'some_handler', get_template_directory_uri() . '/js/your_javascript.js' );
$dataToBePassedtoJS = array(
'language_code' => ICL_LANGUAGE_CODE,
'translate_string' => __( 'Translate me!', 'default' )
);
wp_localize_script( 'some_handler', 'php_vars_for_js', $dataToBePassedtoJS );
// the 'php_vars_for_js' will be an object in JS,
// it's properties will be the content of the dataToBePassedtoJS array.
在你的javascript
(function($) {
"use strict";
// get the ICL_LANGUAGE_CODE passed by wp_localize_script's 'php_vars_for_js' to your JS:
var getLangCode = php_vars_for_js.language_code;
// show it in the console, just for fun
console.log ('ICL_LANGUAGE_CODE passed from WordPress: ' + getLangCode);
// so now you have the getLangCode, you can use it for your conditional
if (getLangCode === 'en') {
var imagesPath = 'https://website-domain.com/file-name.jpg';
} else if (getLangCode === 'fr') {
var imagesPath = 'https://website-domain.com/fr/file-name.jpg';
}
}
}(jQuery));
这样,您可以从 WordPress PHP 获取任何需要的变量以传递给您的 JS。
我正在尝试使用 WPML 在 wordpress 的 .JS 文件中有条件地显示 link。我已经尝试了以下但现在正在工作;
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
if (getLangCode === 'en-US') {
var imagesPath = 'https://website-domain.com/file-name.jpg';
}else if (getLangCode === 'fr-FR') {
var imagesPath = 'https://website-domain.com/fr/file-name.jpg';
}
我在上面的行中收到以下错误 Uncaught SyntaxError: Unexpected token <
我正在使用 link 将图像和文本添加到容器中...文本工作正常,因为我使用 wp_localize_script
使字符串可翻译但是当我切换到法语,图像不再显示,因为 link 现在包含 fr
如能帮助解决此问题,我们将不胜感激
尝试: 1) 删除这一行:
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
2) 将此代码添加到您的 functions.php
add_action('wp_head', 'change_this_name');
function change_this_name() {
?>
<script type="text/javascript">
var getLangCode = <?php echo '"' . ICL_LANGUAGE_CODE . '"' ; ?>; //WPML code to detect site's language. Getting error on this line
</script>
<?php
};
由于您已经在使用 wp_localize_script,我将使用它不仅传递翻译,还传递您的语言代码,如某些评论和教程中所述。像这样:
在 WordPress 中
wp_enqueue_script( 'some_handler', get_template_directory_uri() . '/js/your_javascript.js' );
$dataToBePassedtoJS = array(
'language_code' => ICL_LANGUAGE_CODE,
'translate_string' => __( 'Translate me!', 'default' )
);
wp_localize_script( 'some_handler', 'php_vars_for_js', $dataToBePassedtoJS );
// the 'php_vars_for_js' will be an object in JS,
// it's properties will be the content of the dataToBePassedtoJS array.
在你的javascript
(function($) {
"use strict";
// get the ICL_LANGUAGE_CODE passed by wp_localize_script's 'php_vars_for_js' to your JS:
var getLangCode = php_vars_for_js.language_code;
// show it in the console, just for fun
console.log ('ICL_LANGUAGE_CODE passed from WordPress: ' + getLangCode);
// so now you have the getLangCode, you can use it for your conditional
if (getLangCode === 'en') {
var imagesPath = 'https://website-domain.com/file-name.jpg';
} else if (getLangCode === 'fr') {
var imagesPath = 'https://website-domain.com/fr/file-name.jpg';
}
}
}(jQuery));
这样,您可以从 WordPress PHP 获取任何需要的变量以传递给您的 JS。