Skip to content Skip to sidebar

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

Problem:
The client is unable to translate strings from a PHP file on their website because the strings are not wrapped in the necessary WordPress functions (__() or _e()).
Solution:
We recommend wrapping the hard-coded strings in the PHP file with GetText functions to make them translatable. You can find detailed instructions on how to find strings that don't appear on the String Translation page and how to wrap texts in GetText calls here:

The __() function retrieves a translated string without displaying it, useful for assigning to variables or complex operations:

$translated_string = __('Hello, world!', 'text-domain');

The _e() function retrieves and displays a translated string immediately, useful for simple outputs:

_e('Hello, world!', 'text-domain');

If these functions are missing, it indicates that the theme may not have been developed with translation in mind. For automated assistance in adding these functions, you might consider using tools like PHP Code Beautifier and Fixer (PHPCBF) alongside PHP CodeSniffer (PHPCS), though manual adjustments might still be necessary for complex cases.

If this solution does not apply to your case, or if it seems outdated, please check the related 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: 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 5 replies, has 0 voices.

Last updated by Bruno Kos 1 year, 5 months ago.

Assisted by: Bruno Kos.

Author Posts
October 28, 2024 at 1:56 pm #16338006

craigR-9

Thanks Mihai. Appreciate all that information. I have translated the home page and the option to switch to Japanese appears in the footer now, however when switching to Japanese the page's content is blank. Any thoughts on why this might be?

Thanks for all your help.

October 31, 2024 at 9:19 am #16350335

Carlos Rojas
WPML Supporter since 03/2017

Languages: English (English ) Spanish (Español )

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

Hi there,

Before one of my colleagues takes this ticket I would like to ask you to follow these steps:
- Edit the homepage in the original language
- Make a small change to the content
- Save the changes and update the translation
- Double-check if the issue disappeared

If you are using templates to display the content of the homepage, then double-check if the template is translated and published.

Please provide the results of the steps described above so my colleague can continue working on this ticket.

November 3, 2024 at 9:28 pm #16358465

craigR-9

Hi Carlos,

It worked, sort of. Some content is appearing, some of it translated, some not. Most of this content is created with Custom Fields, so I'll take a closer look at that documentation.

A couple of things point to the WPML installation not working as it should have, could I get your input on that? I'm not sure whether I should work granularly through piece of content to fix it (there's a lot), or just reinstall WPML and try starting over again.

For example, I selected 'automatic translation' during the install process, which didn't work. Support staff Mihai Apetrei was able to determine this was because WPML set itself up with the Classic Translation Editor rather than the Advanced Translation Editor. I assume the ATE is usually set by default, so I'm wondering why this happened.

If WPML was installed with CTE, is it possible there are other settings that have been set incorrectly?

Craig

November 4, 2024 at 2:30 am #16358653

craigR-9

Hi again,

In addition to my earlier reply: Following a conversation with the dev, I see a lot of the home page content is coming from a functions.php file.

I wondered if running string translations might translate that content, but running it didn't result in any change.

Should WPML be able to translate the English language strings out of a php file?

Craig

November 4, 2024 at 8:51 am #16359360

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

If this content is coming from hard coded strings, you need to do this:
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#scan-the-theme-or-plugin

But these need to have GetText calls:
https://wpml.org/documentation/support/translating-the-theme-you-created/#wrap-all-texts-in-gettext-calls

If all of the above is done and if the home page translation is still blank, would you be willing to provide me with WordPress and FTP credentials so I could investigate the issue directly?

https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

I marked your next reply as private so that you can safely add credentials.

November 11, 2024 at 12:56 am #16386359

craigR-9

Hi,

We've had some success getting translations to appear following Bruno's comments above.

We're still having issues with theme file strings not being translated. There is an option in WPML for translating theme file strings but we're having trouble with it.

I'm posting here now to keep this ticket open. We should have more information and a request for assistance in the next 24 hours.

Thanks for all your help.

Craig

November 11, 2024 at 6:30 am #16386681

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

We're still having issues with theme file strings not being translated. There is an option in WPML for translating theme file strings but we're having trouble with it.

Would you be willing to provide me with WordPress and FTP credentials so I could investigate the issue directly?

And send me URLS/screenshot to show me which strings are causing the trouble?

https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

I marked your next reply as private so that you can safely add credentials.

November 14, 2024 at 8:02 am #16400947

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

The ticket was created within the 30-day refund period so the refund would be available even after this, but I hope I can help so that this is avoided.

Let me focus on this first:

1. A large amount of strings requiring translation aren't appearing in WPML's String Translation. I believe they require manual wrapping with the GetText call and I've contracted a developer to help with that.

Can you send me an example of such string? Where does it appear in the frontend and where in the theme files?

If the theme is not using https://wpml.org/documentation/support/translating-the-theme-you-created/#adding-gettext-calls-to-php-code, neither multilingual solution would be able to translate them.

November 14, 2024 at 12:55 pm #16402641

craigR-9

Thanks Bruno. From my developer:

"as they have mentioned in their response, if the string does not have __() or _e() function used with it it wont be picked by any translation plugin.

This means the code is not written in a translatable way. I think considering that the only solution is to fix these string manually by wrapping them in __() or _e() depending on the usage."

It looks like there's a substantial amount of work to add the functions throughout the site. Would these functions normally already exist in a WP site's code, Bruno?

November 14, 2024 at 1:48 pm #16402938

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

Yes,the __() and _e() functions are standard parts of WordPress and are essential for making strings translatable in WordPress themes and plugins.

__(): This function retrieves a translated string but does not display it directly. It is used when you need to assign the string to a variable or use it in a more complex way, such as concatenating it with other strings.

$translated_string = __('Hello, world!', 'text-domain');

https://developer.wordpress.org/reference/functions/__/

_e(): This function retrieves a translated string and echoes (displays) it immediately. It is shorthand for echo __('...').

_e('Hello, world!', 'text-domain');

https://developer.wordpress.org/reference/functions/_e/

If these functions are missing, it likely means the theme was not developed with translation in mind. The developers might not have prioritized internationalization (i18n) or weren't familiar with the WordPress standards for making content translatable.

You can automate parts of the fixing process using tools like PHP Code Beautifier and Fixer (PHPCBF), which works alongside PHP CodeSniffer (PHPCS). But this is for our https://wpml.org/contractors/ and this process may not handle every case, especially complex or concatenated strings.

It's normal for static strings to be translatable, but ideally, most translatable content comes from dynamic sources like posts and pages so it is a bit unclear to me why so many parts of the theme were coded this way.

November 14, 2024 at 9:39 pm #16405177

craigR-9

Ok thanks Bruno. Understood and we'll continue working on those string compatibility fixes.

2. A large amount of content translation for this website needs to be managed in String Translations, however the taxonomy of the strings seems random and it's difficult to know which string is being used where.

In dashboard WPML > String Translation, the domain dropdown filter isolates strings associated with my theme ('ghoul'), however the organisation of strings from there seems random (screen attached).

Is there any way strings can be organised better, for example by the pages they appear on?

1.jpg
November 18, 2024 at 6:57 am #16412956

Bruno Kos
WPML Supporter since 12/2018

Languages: English (English ) German (Deutsch ) French (Français )

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

For hard coded strings it would not work, it would only show them listed under the domain.

It works only when page builders are used, it creates a package of strings then based on the page ID and the builder used.

packages.jpg