Score:2

Translation of a multilingual site into English

sa flag

Note: This question is only focused on strings inside source files (PHP files).

When you are creating a Drupal site in a non English country (let say France), you will create all your PHP files with French strings inside.

After that, you can translate this site, let say in Dutch. You will extract PO files, send them into translation an re-import them.

Until this point, everything is fine.

Now, your client wants to have the site also translated into English.

As you may know, all the source files (with French strings) are in fact considered to be in English by the system.

This means that to handle this fact, I have to:

  1. Extract the PO files to translate into French

  2. Convert all source files with french strings into the same source files but with English strings (using the translation created at step 1)

  3. Revert the languages of the PO file created at step 1 (source language becomes target language and the opposite)

  4. Import the reverted file created at step 3 into the site

If you think I am completely wrong with this process, feel free to explain me why. The most complicated step is step 2: Do you know a smooth process or a nice tool to handle it?

I assume I will find something for step 3, but any idea is welcome.

Sohail avatar
ru flag
A best practice is to always consider the strings in your code to be English from the first step and then translate those to your primary language, let's say, French but yeah if I were you, I'd swap my strings to "English" and proceed from there
apaderno avatar
us flag
Drupal translation system works when the source string is in English. If I call `t()` as `t('Sì, questa è una frase in italiano.")` the string won't be translated because none of the source strings will contain phrases that aren't in English.
Baud avatar
sa flag
@Sohail: the client do not want to pay for a translation into English if he didn't need it... But you are right... best practice! Any idea for a process to swap strings in php files?
Baud avatar
sa flag
@apaderno: It working well until you add English as an added language of the site. Until this point you can do t("une phrase en français") it will works nicely
apaderno avatar
us flag
Yes, but because it returns the same string you passed to `t()`, when a translation isn't found. Clearly, it doesn't find a translation for `'une phrase en français'` because *une phrase en français* is not an English sentence. That isn't translating.
apaderno avatar
us flag
`t()` is always to translate from English to the language set as default from the site, the language users set as their language, or the language set for a node. It doesn't translate from Italian to Dutch, for example, or French to English. That's documented in [`t()`](https://api.drupal.org/api/drupal/core%21includes%21bootstrap.inc/function/t/8.9.x). ***string $string**: A string containing the English text to translate.*
Baud avatar
sa flag
@apaderno you are right, but you can understand the a client doesn't want to pay to translate a French site into English only to have the source file in English (assuming he doesn't need the site in English) If you agree on this, you can understand where I am.
apaderno avatar
us flag
You don't need to translate a site from French to English: All the modules use strings in English, so the site is already "translated" in English. If somebody is passing to `t()` a string in French, it's the "fault" of that person.
Baud avatar
sa flag
@apaderno: no, I need to translate it as I have several custom modules with thousand of french strings in it...
apaderno avatar
us flag
Edit the strings that those modules pass to `t()` to contain English phrases, and the site will appear in English, when English is set as default language.
Score:1
de flag

It is indeed best practice to have the strings in your code being written in English (I will not discuss that point).

Yet, I had a similar issue once with a website (Drupal 7) a colleague of mine developed. The customer initially wanted to have the website in NL and FR and only many years later decided to add an EN version.

Problem: Strings in the code were a mix of NL and FR...

The workaround I found was to activate the "English, British" language (and deactivate the "English" core language since default language was NL). That way, all your strings can be translated into en-gb from the interface translation or from import of a .po file.

In Drupal 8/9, it is even easier as there is a box labelled "Enable interface translation to English" on page admin/config/regional/language/edit/en.

Baud avatar
sa flag
This is, for now, the best try to answer my question... I am very happy about the new Drupal 8/9 feature (unfortunately, this is a D7 site). I think I will not use this trick because I have many other content already translated in English (Node etc) I am afraid to convert all this stuff in EN-GB... But, thank you this is a nice tip. (I rewarded you)
Score:1
ru flag

As mentioned in the comments, Always start with English string and translate that even if it's a one language only website, to ease the pain here's what I always do to keep the strings and translations contained to the theme itself:

open your THEMENAME.info.yml file and add the following:

'interface translation project': THEMENAME
'interface translation server pattern': 'themes/custom/THEMENAME/translations/%language.po'

then create a translations directory within your theme and there you can add your translation .po files, eg. fr.po for french or nb.po for Norwegian

This is the syntax you need to have within your .po file:

msgid "Hello World"
msgstr "Bonjour le monde"

So whenever you write Hello World string in a t() function or as a filter in twig file like so: {{ Hello World|t }} it would know to import the translations from there

But how to trigger the translation import?

drush locale:check - not necessary, but checks if there are any new strings added

drush locale:update - Imports the translations and you're done

Note that you can add/run this to your deployment procedure as well and make your life easier, no need for UI translation, just add your string to your .po file whenever you write hardcoded string into your files

Update: Didn't even notice we are talking Drupal 7, this refers to Drupal 8 for future readers

Baud avatar
sa flag
I have rewarded your answer even if it is not answering my question because I learned many things from it
Sohail avatar
ru flag
Thanks, didn't even notice we are talking Drupal 7, this refers to Drupal 8
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.