Skip Navigation

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

Problem:
The client is experiencing an issue where strings in the functions.php file are not displaying in the correct language despite using the correct WPML string setup. The issue occurs when using a shortcode that includes translated strings.
Solution:
If you're experiencing this issue, ensure you are using the gettext

__()

function correctly. First, scan your theme for new strings in WPML under Theme and plugins localization. You can find this option at Theme and plugins localization. After scanning, translate the strings accordingly.
If the gettext call is set up correctly and you still face issues, you might want to use the

wpml_translate_single_string

hook to retrieve string translations directly. More details on this hook can be found here: wpml_translate_single_string.
For additional guidance on enabling text translation for themes not compatible with WPML, refer to this documentation.

If this solution does not resolve your issue or seems irrelevant due to being outdated or not applicable to your case, we recommend opening a new support ticket. We also highly suggest checking related known issues at known issues, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. For further assistance, please visit our support forum at 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.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Andrey 6 months, 2 weeks ago.

Assisted by: Andrey.

Author Posts
May 15, 2024 at 1:07 pm #15630549

Thomas Henninger

Hello dear Team,

It seems that there is an Issue with Strings in functions in the functions.php in wordpress that you wpml string setup .. it gives back the correct $lang .. but the translation is always the default langauge..

Basically its about a function in functions.php:

with theese:

$output .= '<div class="cell heading-cell">' . __('Rabatt (Prozent)', 'discount-table-shortcode') . '</div>';

If have added the correct string tranbslation, but the langauge is never the one from the page ..

I use this as a shortcode on various pages..

How can I display wpml strings simply with the correct langage code?

This is the complete code:

// Shortcode function to display discount percentages and discounted prices for all quantity ranges for a specific product
function display_discount_info_shortcode($atts) {
// Set the language for WPML
if (function_exists('icl_get_languages')) {
$languages = icl_get_languages('skip_missing=0');
if (isset($languages[ICL_LANGUAGE_CODE]['default_locale'])) {
$lang = $languages[ICL_LANGUAGE_CODE]['default_locale'];
echo $lang;
switch_to_locale($lang);
}
}

// Extract shortcode attributes
$atts = shortcode_atts(array(
'id' => '', // Default product ID
), $atts, 'display_discount_info');

// Get the product ID from shortcode attribute
$product_id = $atts['id'];

// Get the product based on ID
$product = wc_get_product($product_id);

// Check if product exists
if ($product) {
// Initialize output variable
$output = '';

// Define quantity ranges and their corresponding discounts
$quantity_ranges = array(
array('min' => 1, 'max' => 5),
array('min' => 6, 'max' => 10),
array('min' => 11, 'max' => 24),
array('min' => 25, 'max' => PHP_INT_MAX) // PHP_INT_MAX represents an arbitrary large number
);

// Get the sale price of the product
$sale_price = $product->get_price();

// Start the "table" container
$output .= '<div class="discount-info-table">';

// Heading row for Quantity Range
$output .= '<div class="heading-row">';
$output .= '<div class="cell heading-cell">' . __('Menge', 'discount-table-shortcode') . '</div>';

// Loop through each quantity range to create cells
foreach ($quantity_ranges as $range) {
// Determine the maximum value for display
$max_display = ($range['max'] === PHP_INT_MAX) ? '∞' : $range['max'];

// Quantity range cell
$output .= '<div class="cell">' . $range['min'] . ' - ' . $max_display . '</div>';
}
$output .= '</div>'; // End heading row

// Heading row for Discount Percentage
$output .= '<div class="heading-row">';
$output .= '<div class="cell heading-cell">' . __('Rabatt (Prozent)', 'discount-table-shortcode') . '</div>';

// Loop through each quantity range to create cells
foreach ($quantity_ranges as $range) {
// Get the discount price for the current quantity range
$discount_price = apply_filters('advanced_woo_discount_rules_get_product_discount_price', $sale_price, $product, $range['min']);

// Calculate the discount percentage
$discount_percentage = ($sale_price - $discount_price) / $sale_price * 100;

// Discount percentage cell
$output .= '<div class="cell">' . $discount_percentage . '%</div>';
}
$output .= '</div>'; // End heading row

// End the "table" container
$output .= '</div>'; // End discount-info-table

// Restore the original language
if (function_exists('restore_previous_locale')) {
restore_previous_locale();
}

// Return the output
return $output;
} else {
// Restore the original language
if (function_exists('restore_previous_locale')) {
restore_previous_locale();
}

return __('Produkt mit der angegebenen ID nicht gefunden.', 'discount-table-shortcode');
}
}

// Register the shortcode
add_shortcode('display_discount_info', 'display_discount_info_shortcode');

isssue.jpg
May 15, 2024 at 8:32 pm #15632267

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kyiv (GMT+02:00)

Thank you for contacting WPML support.

We are unable to debug your custom code as it is beyond the scope of the support provided here. However, it seems that in your code, you are using the function switch_to_locale($lang), which could be causing incorrect translations to be displayed.

How can I display WPML strings simply with the correct language code?

To address this question, I would suggest removing any locale switching and checking if this helps.

For more information, you can refer to the following resources:
WPML Coding API
https://wpml.org/documentation/support/wpml-coding-api/

WPML Hooks Reference
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/wpml-hooks-reference/

May 16, 2024 at 8:54 am #15633856

Thomas Henninger

Hello Andrey,

I tried different things, basically nothing worked. You dont need to debug the code.

Can you only help me with following:

How can I get with a php function and the langauge code a specific String translated by stringtranslation wpml back in a certain language?

Do you know what I mean?

Thank you

May 16, 2024 at 12:34 pm #15635037

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kyiv (GMT+02:00)

Thank you for your feedback.

If you are not using the gettext __() call, as explained here: https://wpml.org/documentation/support/enabling-text-translation-for-themes-not-compatible-with-wpml/, you can try using the wpml_translate_single_string hook to retrieve a string translation: https://wpml.org/wpml-hook/wpml_translate_single_string/.

However, if you are using the gettext call correctly even in functions.php, after scanning the theme for new strings in WPML → Theme and plugins localization https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#scan-the-theme-or-plugin and translating them, the string translation should be displayed correctly according to the language on the website.

Additional information:
https://wpml.org/documentation/support/translating-the-theme-you-created/
https://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/

I hope this helps.