2

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.

10
  • 4
    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
    – Sohail
    Commented Oct 12, 2021 at 10:40
  • 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.
    – avpaderno
    Commented Oct 12, 2021 at 10:40
  • @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
    Commented Oct 12, 2021 at 12:26
  • @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
    – Baud
    Commented Oct 12, 2021 at 12:28
  • 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.
    – avpaderno
    Commented Oct 12, 2021 at 12:44

2 Answers 2

1

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.

1
  • 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)
    – Baud
    Commented Oct 13, 2021 at 15:40
1

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

2
  • 1
    I have rewarded your answer even if it is not answering my question because I learned many things from it
    – Baud
    Commented Oct 13, 2021 at 15:35
  • Thanks, didn't even notice we are talking Drupal 7, this refers to Drupal 8
    – Sohail
    Commented Dec 7, 2021 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.