Skip Navigation

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

Problem:
The client created a custom function to simplify adding translations in their code, but the translations are not being picked up by WPML's scanning process because the function does not use the standard format recognized by WPML.
Solution:
1. Ensure that the text domain is passed as a string directly in the translation functions, as using variables or constants can prevent WPML and other tools from correctly recognizing the text domain. For more details, see the WordPress developer resources on internationalization: Internationalization guidelines.
2. To manually register strings that are not picked up automatically, use the

wpml_register_single_string

action. This is particularly useful for dynamic text strings such as user inputs. For more information on how to use this action, visit: wpml_register_single_string documentation.
3. To retrieve a string translation, refer to the

wpml_translate_single_string

action. Documentation can be found here: wpml_translate_single_string documentation.

If this solution does not apply to your case, or if it seems outdated, we recommend opening a new support ticket. We also highly recommend checking related known issues at https://wpml.org/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: 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 3 replies, has 2 voices.

Last updated by Andrey 1 year ago.

Assisted by: Andrey.

Author Posts
June 3, 2024 at 4:50 pm #15698717

nathanR-5

I have made a function to avoid typing

__('...', 'domain-name')

every time I want to add a new translation.

These will not be picked up when scanning the site however - I did some digging and found the translations are adding in potx.php which scans files for the typical code written when adding a translation.

As my function (in fact its a blade component, but that doesn't matter) is something like:

function tx(string $text) { 
   echo __($text, 'hardcoded-domain'); 
}

It obviously doesn't work with the potx.php file.

How can I manually register a new string with WPML?
I can hook into the `gettext_{domain}` filter, but how do I programmatically register the found text/domain pair with WPML?

June 3, 2024 at 7:30 pm #15699087

Andrey
WPML Supporter since 06/2013

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

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

The gettext function expects a string parameter for the $domain. It will not correctly work with variables or constants.

https://developer.wordpress.org/reference/hooks/gettext/

Here are more details:
https://developer.wordpress.org/themes/functionality/internationalization/

The text domain should be passed as a string to the localization functions instead of a variable. It allows parsing tools to differentiate between text domains. Example of what not to do:

__( 'Translate me.' , $text_domain );

I cannot suggest anything other than that.

June 4, 2024 at 8:12 am #15700076

nathanR-5

I found a workable solution but am curious if anyone can see any issues with it:

use WPML\ST\Gettext\Settings;
use function WPML\Container\make;

add_filter('gettext_kikk', 'manuallyRegisterStrings');

function manuallyRegisterStrings($translation, $text, $domain)
{
    $settings = make( Settings::class );
    
    if ( $settings->isAutoRegistrationEnabled() ) {
        do_action( 'wpml_register_single_string', $domain, sanitize_title($text), $text);
    }
    
    return $translation;
}
June 4, 2024 at 8:22 am #15700128

Andrey
WPML Supporter since 06/2013

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

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

We have the action wpml_register_single_string, https://wpml.org/wpml-hook/wpml_register_single_string/. This action is typically used for user input texts, also known as “dynamic text strings”.

To retrieve a string translation, please refer to: wpml_translate_single_string, https://wpml.org/wpml-hook/wpml_translate_single_string/.