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.

Support

Translation via Twig always fallback on English

Started by Stefan 3 years ago · 2 replies · 391 views
3 years ago

Hi,

I'm creating multilang site with default language SR and EN, FR, DE supported also. I'm using twig translation filter (|t), all strings in twig templates are written in SR and I don't have sr.yaml file. I choose this approach because there is not much content but some labels/tags etc...

Problem is that on all sr pages twig translates strings to EN. I suppose it does that because no sr.yaml language file is present since I don't need to translate strings that are already in that language.

my config:

MARKDOWN
languages:
  supported:
    - sr
    - en
    - fr
    - de
  default_lang: sr
  include_default_lang: true
  include_default_lang_file_extension: true
  translations: true
  translations_fallback: false
  session_store_active: false
  http_accept_language: false
  override_locale: false
  content_fallback: {
    sr: sr
  }
  pages_fallback_only: false

Is this bad approach or there are some configs that I'm missing?? Thanks!!

last edited 05/09/23 by pamtbaau
3 years ago

@hef, A few points...

all strings in twig templates are written in SR

Usually, strings in Twig that need to be translated have a dotted notation, like {{ 'THEMENAME.TOPIC.TITLE'|t }}.
In the language.yaml file you would then have:

YAML
en:
  THEMENAME:
     TOPIC:
        TOPIC: The title of the topic
sr: 
  THEMENAME:
     TOPIC:
        TOPIC: Наслов теме

You say your strings are in Serbian. How do your Twig strings look like?

I suppose it does that because no sr.yaml language file is present since I don’t need to translate strings that are already in that language.

Usually, a theme/plugin makes use of a language.yaml file which contains the strings that need to be translated as shown above.

Only when overriding translations provided by the theme/plugin one can use files like user/languages/sr.yaml which contain the strings to be overridden.
Are you overriding translations provided by the theme/plugin?

Remarks like "all strings in twig templates are written in SR" and "no sr.yaml language file is present" are smells of a bad multilanguage infra structure.

As a kind request, please improve legibility by formatting code or yaml snippets using triple backticks.

3 years ago

@pamtbaau

You say your strings are in Serbian. How do your Twig strings look like?

I am aware that strings usually have dotted notation in twig but I tried to bypass this and hardcode strings directly in twig. (maybe not smartest idea)
Then I translated whole string in other language files eg: en.yaml:

TXT
"Наслов теме": "The title of the topic"

What happens is that even when I'm on SR version of page this is still translated to EN when I expect to remain untranslated. That's why I wrotte that I suppose it automatically translates to EN when no SR.yaml is present.
I have configured default locale to SR and default lang to SR.

I don't want to further overcomplicate things, I will find solution with better practice. Just wanted to check if this is something thats related to config or just how things works.

Thanks again!

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
3 90 1 hour ago
Support · by Anna, 3 days ago
2 92 22 hours ago
Support · by Justin Young, 23 hours ago
1 40 23 hours ago
Support · by Duc , 1 week ago
2 98 6 days ago
Support · by Colin Hume, 1 week ago
2 90 6 days ago