1

How to add a translation file to a custom theme?

I did

my_theme.info.yml

'interface translation project': my_theme
'interface translation server pattern': themes/my_theme/translations/my_theme-%language.po

twig template:

 {% trans %}
  status
{% endtrans %}

{{ 'status'|t }}

/my_theme/translations/my_theme-fr.po:

msgid "status"
msgstr "statut"

msgid "statut"
msgstr "status"

followed by a

drush cr

The output is not translated when I am on the fr page (the default language being english).

3 Answers 3

1

The .po file is not imported by being placed in the themes directory, you need to import it. To do that go here:

Home > Administration > Configuration > Region and Language > Import

2
  • in /admin/config/regional/translate/import it says : "Les fichiers de traduction sont automatiquement téléchargés et importés quand des langues sont ajoutées, ou quand des modules ou thèmes sont activés.". This does not work when i activate the theme
    – Matoeil
    Oct 11, 2018 at 15:20
  • @Jdrupal, turns out you can import the PO from your theme or module, I didn't know either when I replied initially. api.drupal.org/api/drupal/… Oct 11, 2018 at 17:55
1

Your implementation seems to follow the guidelines on the locale API documentation, but nevertheless, it wasn't working for me either.

What worked for me was implementing hook_locale_translation_projects_alter on a module:

function mymodule_locale_translation_projects_alter(&$projects) {
  $projects['my_theme'] = array(
    'info' => array(
      'interface translation server pattern' => 'themes/custom/my_theme/translations/my_theme-%language.po',
    ),
  );
}

I tested by enabling the module first, then enabling adding the language via drush command:

drush language-add fr

This is my PO file, Drupal seems to require a heading section:

# French translation of Drush Site-Install
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2018-10-11 16:30+0000\n"
"PO-Revision-Date: 2018-10-11 16:30+0000\n"
"Last-Translator: NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <EMAIL@ADDRESS>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

msgid "status"
msgstr "statut"

msgid "statut"
msgstr "status"

Without the header, it was skipping the first translation and I was getting this error:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /batch?id=14&op=do_nojs&op=do
StatusText: OK
ResponseText: Notice: Undefined offset: 1 in Drupal\Component\Gettext\PoHeader->parseHeader() (line 257 of /app/core/lib/Drupal/Component/Gettext/PoHeader.php). => Array ...
5
  • 1
    in /admin/config/regional/translate/import it says : "Les fichiers de traduction sont automatiquement téléchargés et importés quand des langues sont ajoutées, ou quand des modules ou thèmes sont activés.". This does not work when i activate the theme
    – Matoeil
    Oct 11, 2018 at 15:20
  • i have managed to set the automatic import of custom module po files on activation but not theme. are you sure it has to be manual?
    – Matoeil
    Oct 11, 2018 at 15:38
  • 1
    I was looking a bit more into this, I think I am wrong and you can actually attach your translations to a module or theme, let me just update my answer. See api.drupal.org/api/drupal/… Oct 11, 2018 at 15:43
  • thanks but my info.yml properties above does not work
    – Matoeil
    Oct 11, 2018 at 15:55
  • also manually import of previsouly imported and modified po files do not override the previous translation !?
    – Matoeil
    Oct 11, 2018 at 15:57
0

Thanks to what @Cesar Moore said and executing drush locale:check then drush locale:update and drush cr, my translation file is now working for my custom theme too :)

FYI : my PO file is in a custom module folder.

More refs :

Your Answer

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

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