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

Last updated by Alejandro 4 weeks, 1 day ago.

Assigned support staff: Alejandro.

Author Posts
September 6, 2019 at 10:38 am #4524863

marianneC-2

The website is built on WordPress, with all pages done using PHP. All text is hard-coded in the PHP files. It was not originally set up as a multilingual website, and I am now in the position of making it so. I have used <?php _e( 'Your String', 'your-context'); ?> to retrieve all strings for translation, and this works nicely, and all strings are appearing for translation. The issue I have is the likes of this:

<h2 class="headtitle">Welcome to your <?php echo $current_user_pathway_name; ?> Content</h2>

and

<h2 class="headtitle">New <?php echo $current_user_pathway_name;?> Activities</h2>

English is the default language, and Irish is the target language. However, both languages have a different syntax. For example, if Irish were to be literally translated to English, it would appear in English as:

Activities (user_pathway_name ) New

Right now, I have 'activities' and 'new' as separate strings:

<?php _e( 'New ', 'twentynineteen'); ?> <?php echo $current_user_pathway_name;?> <?php _e( ' Activities', 'twentynineteen'); ?>

But this makes them appear in the order they would in English, and reads incorrectly in Irish. Is there a way to work around this in PHP, keeping the strings as one, and leaving an option for moving the user_pathway to be moved around the string according to the conventions of the target language?

Thank you!

September 6, 2019 at 12:52 pm #4525387

Alejandro
Supporter

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

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

Hello!
Welcome to WPML Support.
I'll do my best to help you solve this issue.

could you tell me where would $current_user_pathway_name come from? is it taking a path from a field on the admin panel the user can change or write into?

It all depends where is that variable coming from because if the variable is something like:

$current_user_pathway_name = "my string"

then you would just have to wrap "my string" around the get text call, but if that's not the case it will depend on where is the actual value of the variable coming from.

Let me know so i can better understand your question and try to help you find an answer to it.

September 6, 2019 at 1:19 pm #4525453

marianneC-2

Hi Alejandro,

Thank you, I appreciate your support here.

These are custom taxonomies created within WordPress. Does that help, or do you need more information here?

September 6, 2019 at 1:25 pm #4525459

Alejandro
Supporter

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

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

Well, if they are custom taxonomies then i believe what you need to do is:

Go to WPML > Settings > Taxonomy Translation > Check if the custom taxonomies are there and then set them as "translatable"

Then, you'd go to WPML > Taxonomy Translation and translate them there.

Beware, though, it all depends on how the taxonomy was created and how, i mean, if it had custom fields then you should also check the taxonomy terms translations in the same page i described above, etc.

Try it out and let me know how it goes.

September 6, 2019 at 1:34 pm #4525551

marianneC-2

Thanks, Alejandro.

I think I may not have been clear here. I can, and have made them translatable, and they are translating just fine. The issue is the text coming from the PHP file. If you look at the line I gave above, and see it as three parts:

<?php _e( 'New ', 'twentynineteen'); ?> <?php echo $current_user_pathway_name;?> <?php _e( ' Activities', 'twentynineteen'); ?>

Part 1: New

Part 2: user_pathway (STEAM or Digital Media are the pathways when viewed live on the site)

Part 3: Activities

On the website, 'New STEAM Activities' or 'New Digital Media Activities' will be seen, depending on what pathway the user has selected.
The Irish for these words are Nua (New), and Gníomhaíochtaí (Activities). When translated, the words do not appear in the same order as in English. It will return 'Nua user_pathway Gníomhaíochtaí', where as we need it to return 'Gníomhaíochtaí user_pathway nua'. Does this make sense?

September 6, 2019 at 1:57 pm #4525683

Alejandro
Supporter

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

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

2 questions:

1) does "user_pathway" gets the correct translation as well in Irish? it looks as if it does, but i just want to be 100% sure of it.

2) by the way you created the get text calls, if NEW comes first, USER_PATHWAY comes second and ACTIVITES comes third, you will always get the translation in the same order by using the structure you're using, which is not what you want.

In this case, try to use sprintf() instead, as you can read in this ticket: https://wpml.org/forums/topic/simple-question-how-to-make-sprintf-and-printf-strings-translatable/#post-1531641

that way you can translate it as a phrase and not as a string, and you can move around the strings to suit the languages' Structure.

Let me know how it goes.

September 12, 2019 at 2:16 pm #4561647

marianneC-2

Thank you, that's exactly what I was looking for. I've seen examples in other languages, and was sure there must be a way to do it in PHP.

I have put that in, looking like this:
<?php echo sprintf ("Welcome to your %s Content", $current_user_pathway_name); ?>

Is this correct? The English displays correctly, but I can't see where to translate the Irish. Where should I see this, please?

Yes, User Pathways are displaying correctly. There are a couple of smaller items I have yet to find, but I haven't been focused entirely on them just yet.

September 12, 2019 at 5:33 pm #4563761

Alejandro
Supporter

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

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

That should be correct, yes. you should now find the string in WPML > String translation.

It could be hard to find, so please try to search for "Welcome to your" and select the filter option "Search in translation"

See if you can find it that way.

If you don't, then go to WPML > Theme and plugin localization > find and select the theme where the strings are and scan it.

Let me know how it goes.

September 16, 2019 at 1:48 pm #4580931

marianneC-2

I couldn't find the string upon scanning, so instead I wrote like this:

<?php echo sprintf( __ ("Welcome to your %s Content", $current_user_pathway_name ) ); ?>

The string now appears, but the translation does not appear on the website. The original English only appears. Have I missed something?

September 16, 2019 at 5:26 pm #4582879

Alejandro
Supporter

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

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

To continue i think it would be useful to have temporary access, if possible.

I have just activated the appropriate boxes for the credential information i need from you. they are private so only you and i will be able to see them.

I suggest you create a temporary user, set it as an administrator and then add those credentials in the fields mentioned above.

Then, please let me know where you added the sprintf() code and where is it suppose to be visible on the front-end. i'll check it out and then let you know what was going on.

Regards.

September 17, 2019 at 5:21 pm #4590377

Alejandro
Supporter

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

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

Hello,

Could you please check if now you can see it? I'm not sure where is it supposed to be shown in the front-end but it appears correctly in the string translation module now.

The only thing I did was wrap the string parameter of the sprintf() function into a standard __() function

So it would look like this:

sprintf( __('content goes here %s', 'domain'), $data) ; 

Let me know if it worked. And if it didn't please let me know in which url can I see it.

September 17, 2019 at 8:47 pm #4590999

marianneC-2

That worked, thank you! You wouldn't have seen it, as you wrote it into a test page I had for playing around the landing page. I put it the code into the landing page, and it worked perfectly.

I wonder was anything else changed? Perhaps on the WordPress end? It's throwing up an internal server error. I removed the test php file, and so all original files remain that I had earlier, which leads me to think there is something on the WordPress end that has changed. The landing page will appear fine, but when I refresh, or move between pages it returns the error. Could you advise please?

September 18, 2019 at 2:54 pm #4597763

Alejandro
Supporter

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

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

It wasn't your fault at all. if anything the problem was on my end.

You see, sprintf() just returns a String but it doesn't do anything on the localization front but it would just solve the first piece of the puzzle: the re-ordering of the words according to the languages and the new variables that you can dinamically set now.

The second part was easier now, because you just had to fuse the sprintf with the localization function and voila! you'd get the string localized and arranged as you want according to the language.

So, as you can see the function is actually a "sprintf( __() ) " so a localized function inside the string printf function

Regards.