Please make sure to update to WPML to the latest and check our list of Known Issues before reporting

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 6 replies, has 2 voices.

Last updated by christophe 2 years ago.

Assigned support staff: Otto.

Author Posts
February 22, 2018 at 3:55 pm #1526423

christophe

Hi

I'm trying to register a string translation in my theme and I want to add the translations for this string through my code.

While googling for this I found this ticket in the forums: https://wpml.org/forums/topic/php-api-to-add-string-translations/

I do understand how to implement this, but I want to know a bit more about the internal working of this function. If I understand it correctly calling this function will add or update the string translation in the database?
So if I'm calling this on init it would write to the database on every request?
So I would like to know if this actually writes to the database or not?

If so I think it would be best practice for me to create a wp cli command in my app to add these translations once using this function?
After that the translation stored in the database will be used?

Thanks
Joren

February 22, 2018 at 7:56 pm #1526724

Otto
Supporter

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

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello Joren,

Those functions were deprecated.

I suggest you check our new API:
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#top

For hardcode strings in your theme, you just need to use gettext functions and follow WP standards:
https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext

For user input strings:
https://wpml.org/wpml-hook/wpml_register_single_string/
https://wpml.org/wpml-hook/wpml_translate_single_string/

Let me know if this points you in the right direction, please.

Kind Regards,

Otto

February 23, 2018 at 10:37 am #1527353

christophe

Hi Otto

Thanks for your answer and the provided links. I'm just not quite sure that this is what I'm looking for.

What I understand from reading the api documentation from the provided links as this will register a string to be translated through WPML translation management. This is not what I'm looking for.

Maybe the following example will be more clear:

* I have a page, which in the next release has a new static title in the source code, let's say "Hello world"
* I could register this like this: <h1><?php echo __('Hello world', 'mydomain'); ?></h1>
* I can translate this through wpml > string translations in the admin interface, but I will have to this for every environment I deploy to.

So I'm looking for a way to to register translations for this static text (string) in my code that is still changeable through wpml > string translations in the admin interface, and the functions you pointed out are not made for that if I understand the documentation correctly.

Please correct me if I'm misunderstanding the documentation you provided, but otherwise can you point me in the right direction?

Thanks
Joren

February 23, 2018 at 11:43 am #1527433

Otto
Supporter

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

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello Joren,

Thanks for the explanation.

In that case, the translation should be included in the .po file.

Before, WPML had only two alternatives:
-Use string translation
-Use .po files

But now you can use both.

WPML will scan it and use it as a fallback if no translation is made in WPML > String translation

So, if I understood properly, your goal will be achieved: the string will be translated in every instance you deploy and it will be possible to be overwritten by the user in WPML > String Translation.

Makes sense or I am missing something?

Kind Regards,

Otto

February 23, 2018 at 3:30 pm #1527704

christophe

Hi Otto

Below a small recap of what I understood:

* Create a string translation like this: <h1><?php echo __('Hello world', 'mydomain'); ?></h1>
* Make sure to add it to the PO file
* As from this point my string is translated in all languages on every server on deploy
* I can now overwrite this using through the admin interface when needed

Correct?

Side question: the string translation plugin is always first checking the po file?

Joren

February 23, 2018 at 5:48 pm #1527848

Otto
Supporter

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

Timezone: America/Argentina/Buenos_Aires (GMT-03:00)

Hello Joren,

Yes, that's correct.

When a theme is activated WPML scans it and scans the .po files, so the translation will be available for every language that has its language file.

If for some reason, maybe the order in which things were added to the site, the string is not scanned, you can do it manually:
1. Go under “WPML -> Theme and plugins localization”
2. Make sure that “Translate the theme and plugins using WPML's String Translation” is active
3. Scroll down to “Strings in the theme” and click on “Scan the theme for strings”

and, as the string is translated already in the file, it will appear translated in the front end.

Regarding the side question:
WPML checks the .po file first.

Depending on this setting:
WPML > Theme and plugins localization > Localization options > How to translate strings in themes and plugins?

WPML will scan the .po files, store them in ST and use them always from there, not loading the files (to enhance performance)

or it will use String Translation, loads the files and use them as a fallback if the string is not translated.

Or you can just use the .po files.

In the first two cases, you can overwrite the .po file translation translating the string in WPML > String Translation.

Let me know if my explanation was clear enough, please.

Kind Regards,

Otto

February 27, 2018 at 8:18 am #1530106

christophe

Hi Otto

Ok that's clear for me, I have this implementation in my planning in 1 or 2 weeks and will try to implement this then. So for now I don't need any help, and I hope I can get it implemented in 1 or 2 weeks 😉

Thanks
Joren