Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

How to make theme translations work properly?

theme

Solved by P. Mergey View solution

Started by P. Mergey 6 years ago · 8 replies · 933 views
6 years ago

I'm currently trying to set theme translations in order to replace some hard coded strings by variable values.

I'm following step by step the documentation (which theoretically seems very simple). I've created a file named languages.yaml at the root of the theme which contains this test value:

YAML
en:
  THEME_THEMENAME:
    FEED: "Subscribe RSS"
fr:
  THEME_THEMENAME:
    FEED: "Abonnement RSS"

Next, I'm trying to display this value in an element inserted in the base.html.twig file this way:

TWIG
<p>Translation must come here: {{ 'THEME_THEMENAME.FEED' | tu }}</p>

But only the hard coded text Translation must come here: appears...

I tried to use double or single quotes, to clear the cache, to set a languages directory with one file per language: nothing helps. The value is not displayed.

I suppose I have missed a part of the process. Thanks to everyone who would tell me what I forgot to do...

last edited 05/30/20 by P. Mergey
6 years ago

@gizmecano, Should it not be:

TWIG
<p>Translation must come here: {{ 'THEME_THEMENAME.FEED' | t }}</p>

Just in case... THEME_THEMENAME is a placeholder and should be replaced by the name of your theme.

6 years ago

You are absolutely right, but it's a typo in my post and not in my code (I have to correct my initial post).

Nevertheless, as I noted you have used | t and not | tu as I did before, I've just tried with this option: now, it's the key of the value THEME_THEMENAME.FEED which is displayed and not the content of the value...

6 years ago

@gizmecano, When using {{ 'THEME_THEMENAME.FEED' | t }} and the translation filter cannot find the string 'THEME_THEMENAME.FEED' in (any) languages.yaml (or 'user/languages/en.yaml'), it will return the string itself.

I tried the following in theme Quark:

  • Added the following to 'user/themes/quark/languages.yaml'
    YAML
    en:
    THEME_THEMENAME:
      FEED: "Subscribe RSS"
    
  • Added the following to 'user/themes/quark/templates/partials/base.html.twig'
    TWIG
    <p>{{ 'THEME_THEMENAME.FEED' | t }}</p>
    
  • Result:
    HTML
    <p>Subscribe RSS</p>
    

When does it go wrong:

  • When I made a typo in languages.yaml or twig,
  • or the file 'languages.yaml' cannot be found
6 years ago

@pamtbaau: First of all, thank you for your explanations and your own tests.

I think the problem you pointed out about using the | t and not | tu is a good first step. 👍

When I made a typo in languages.yaml or twig,

The content of languages.yaml is exactly the one that I pasted in my initial message. As for the inclusion in the TWIG file, it fits well with the key and value stored in the YAML.

or the file ‘languages.yaml’ cannot be found

Currently, my languages.yaml is set at this path : user\themes\themename\languages.yaml, which seems to exactly match the position you specify for the Quark theme.

When [...] the translation filter cannot find the string in (any) languages.yaml (or ‘user/languages/en.yaml’), it will return the string itself.

Reading your comments (and especially this passage), I therefore deduce that my languages.yaml file (although it seems to be set in the right path) doesn't seem to be found by the system, which returns the name of the string instead of its content.

I suppose I will have to test with the option using a <languages> folder and separate files by languages.

Anyway, I have to try to find why this file is not found by the system...

6 years ago

@gizmecano, To be honest, I have no idea what is happening. The only suggestion I can think of at the moment is installing a fresh Grav installation and replay my steps using Quark and see what happens.

If translations are working in the fresh install, make small changes step by step.

6 years ago Solution

@pamtbaau:
I have no idea what is happening

I have identified my issue: it was not related to the theme files itselves, but to the previous settings of my test blog (which was initially set for a theme without any translation string).

So, the answer was just below in the documention. To properly run, the system.yaml of the website must avec these settings:

YAML
languages:
  translations: true

It was elementary: too focused on the theme development, I had not thought to look at the site settings.

🙄

👍 2
6 years ago

@gizmecano, Good catch!

The setting translations is true by default and I guess that means it doesn't hurt when a site is not multilingual. Therefor, I would never have the inclination to set it to false...

6 years ago

@pamtbaau:
I guess that means it doesn’t hurt when a site is not multilingual.

At the present time, I think like you. This setting dates from a time when I encountered concerns in order to correctly set a default language.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 195 2 months ago
Themes & Styling · by Ian, 2 months ago
3 91 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 451 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 45 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 125 3 months ago