Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client needs to register strings for translation in a PHP shortcode function that displays product stock status. The default language is Russian.
Solution:
If you're experiencing this issue, we recommend placing your code inside the

functions.php

file of your theme. Then, navigate to WPML > Theme and Plugin Localization and scan the theme for new strings. You can find a guide on how to do this here: Scan the Theme or Plugin.
After scanning, translate the strings at WPML > String Translation. This will allow you to use the same shortcode in any language. If the shortcode does not copy automatically when you translate content with the WPML Translation Editor, you may need to place the shortcode manually on your translated content or template.

If this solution does not apply to your case, or if it seems outdated, please check related known issues at https://wpml.org/known-issues/, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. We highly recommend opening a new support ticket if you need further assistance. You can do so here: WPML Support Forum.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Andreas W. 5 months, 1 week ago.

Assisted by: Andreas W..

Author Posts
June 9, 2024 at 1:14 am #15718732

denM

I use PHP shortcodes.
function show_product_stock_status_shortcode($atts) {
global $product;

$atts = shortcode_atts(array('id' => ''), $atts, 'product_stock_status');

if (empty($atts['id'])) {
if (!empty($product)) {
$atts['id'] = $product->get_id();
} else {
return __('Необходим ID продукта.', 'your-text-domain');
}
}

$product = wc_get_product($atts['id']);

if (!$product) {
return __('Продукт не найден.', 'your-text-domain');
}

$stock_status = $product->get_stock_status();

switch ($stock_status) {
case 'instock':
return __('В наличии', 'your-text-domain');
case 'outofstock':
return __('Нет в наличии', 'your-text-domain');
case 'onbackorder':
return __('Доступен под заказ', 'your-text-domain');
default:
return __('Статус запасов: ', 'your-text-domain') . $stock_status;
}
}

add_shortcode('product_stock_status', 'show_product_stock_status_shortcode');
The problem is how to register strings for translation. The default language is Russian.

June 9, 2024 at 3:45 am #15718747

Andreas W.
Supporter

Languages: English (English ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Let's say you place this code inside the functions.php file of your theme, then you need to go to WPML > Theme and Plugin Localization and scan the theme for new strings.

Guide:
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#scan-the-theme-or-plugin

Then translate the strings at WPML > String Translation.

You will then be able to use the same shortcode in any language. The shortcode usually should get copied if you translate the content with the WPML Translation Editor. In case you run into issues with that, simply place the shortcode manually on your translated content or template.

Best regards
Andreas