Skip Navigation

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

There was an error: { "keywords": "WPML, string translation, gettext, custom theme, hardcoded strings", "summary": "Problem:
The client needed to translate hardcoded English strings in a custom theme to Spanish, such as a search box placeholder and button text, and was unsure how to replace these values efficiently.

Solution:
1. We suggested enabling the \"Look for strings while pages are rendered\" option in WPML > String Translation, visiting the page with the string, and then searching for the string in WPML > String Translation.

2. If the strings were not found, we recommended checking if they were registered as admin texts and provided a link to the relevant documentation:
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/

3. We explained that for WPML to recognize strings, they need to be wrapped in a gettext call. We provided an example of how to wrap strings and a link to find the theme's text domain:

__(\'Search Results\', \'your-text-domain\');

https://wpml.org/documentation/support/translating-the-theme-you-created/#text-domains-and-domain-paths-in-wordpress

4. We mentioned that the client's current approach should work but suggested using wpml_current_language instead, with a link to the relevant WPML documentation:
https://wpml.org/faq/how-to-get-current-language-with-wpml/

Please note that the solution provided might be outdated or not applicable to your case. If the issue persists, we 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. If you still need assistance, please open a new support ticket with us." }

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 8 replies, has 3 voices.

Last updated by Bobby 9 months, 1 week ago.

Assisted by: Bobby.

Author Posts
February 20, 2024 at 11:00 pm #15324991

kurtK-7

I'm trying to understand string translation, for example in the header I have a search box with the default text "What are you looking for?" And a default button that says "Search" - How do I change my custom them to replace these English values for their Spanish alternative.

Search button should read 'Buscar' and the input field default should say "¿Qué estás buscando?"

Please let me know as I have other areas in my template where I have English text hardcoded that needs to be updated to Spanish when the user Toggles between the languages.

February 20, 2024 at 11:08 pm #15324995

Mihai Apetrei
Supporter

Languages: English (English )

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

Hi there.

Before this ticket gets assigned to my colleagues, I will try to help.

The proper way to translate strings depends a lot on how they have been implemented inside the theme/plugin.

Sometimes, there are even duplicates of the same string and only one of them is the correct one that needs to be translated.

Also, sometimes, simply removing the string (checking the box and removing it) and then re-registering it again by scanning for it (one of the methods below) and re-translating it resolves the issue.

1. Please try this:

Go to WPML > String Translation and at the bottom of the page you will find a checkbox called "Look for strings while pages are rendered". You can enable (check) that and then go in the front end to a page where you have that string showing up and then go back to WPML > String Translation and search for that string and see if it now shows up.

2. If you can't find them inside the WPML > String Translation, they might be added as admin text.

This should be the normal process to find and translate admin text strings:
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/

Please let us know how that goes.

Kind regards,
Mihai Apetrei

February 21, 2024 at 2:43 pm #15328806

kurtK-7

I read your response. I watched the videos provided but I am still having issues. For example the search results page. In string translation when I search for 'Search Results" or 'Sorry, nothing was found.' I do not see either listed in the String Translation section.

hidden link

February 21, 2024 at 9:30 pm #15330550

kurtK-7

FYI - I have migrated my site from Dev to Staging - Same issue persists - hidden link

February 21, 2024 at 11:57 pm #15330738

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

Hello,

Thank you for providing the details. It's noteworthy that you're using a custom theme.

Did you develop this theme yourself? I must mention that our support scope for custom developments is somewhat limited. Nonetheless, I believe the following guide on String Translation could be useful for making your strings translatable.

WPML String Translation identifies strings that are encapsulated within a gettext function call. This process enables the registration of strings for translation. For a deeper understanding, please visit:
https://wpml.org/documentation/support/translating-the-theme-you-created/#get-started-understanding-gettext-text-domains-and-domain-paths

Let me know your results, please.

February 22, 2024 at 9:36 pm #15335699

kurtK-7

I didn't quite follow the content in that link. I think it was a bit beyond what I am doing as this is a custom template for a client, not a custom template for sale.

Right now I am doing this to address the hardcoded english language in the template so it can also display in spanish. Can you tell me if there is a more efficient way to accomplish the same thing?

This is an example from search.php. This page lives outside of the database content so this is how I am addressing the conversion. Please tell me if there is a better/smarter way.

<h1 class="entry-title">Search Results</h1>

<h1 class="entry-title">
<?php if ( defined( 'ICL_LANGUAGE_CODE' ) && 'en' == ICL_LANGUAGE_CODE ) {
echo 'Search Results';
} else if ( defined( 'ICL_LANGUAGE_CODE' ) && 'es' == ICL_LANGUAGE_CODE ) {
echo 'Resultados de la búsqueda';
} ?>
</h1>

February 22, 2024 at 11:35 pm #15335852

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

Thank you for sharing the above with me.

I think for your specific case use it is important to review the part for Gettext and Wrapping Basic Strings. In order for a string to be picked up by WPML String Translation your hardcoded string needs to be wrapped in a gettext call.

For example:
Not Wrapped --> <h1 class="entry-title">Search Results</h1>

Wrapped ---> <h1 class="entry-title">__('Search Results', 'your-text-domain');</h1>

example of a wrapped string --> __('Hello, world!', 'your-text-domain');

'your-text-domain' = in here you would typically add the theme's text domain, you can see how to find it here:
https://wpml.org/documentation/support/translating-the-theme-you-created/#text-domains-and-domain-paths-in-wordpress

if you can't find it just you can use 'lw-supply' for example.

Once that is done you can then re scan the theme for strings from WPML->Theme and plugin localization.

-------------------

Regarding the code you are using, it's not ideal but it should work.
You might want to use wpml_current_language instead:

https://wpml.org/faq/how-to-get-current-language-with-wpml/

February 23, 2024 at 3:58 pm #15338610

kurtK-7

This was very helpful. I've made changes to my custom theme. Scanned the theme and made the translations in the string editor. Thank You.

Is there a way to do this taxonomy names? I have a taxonomy that I don't want to translate because it seems like I will need to tag all the posts and the content in the posts does not need to be translated but the taxonomy terms that filter the post content should be translated.

hidden link

February 23, 2024 at 9:13 pm #15339397

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

The posts, are you using our feature "Using the default language as a fallback for untranslated content" to display them in the translation language?

Reference: https://wpml.org/documentation/translating-your-contents/displaying-untranslated-content-on-pages-in-secondary-languages/

If that is the case I must add that the filters for the posts will not work as expected as the posts do not actually exist in the database.