Some translations do not work in my template class

You are trying to translate a string with a context but in your po file your string doesn’t declare a context

To declare a context in your po file you need to use msgctxt

msgctxt "permalink title"
msgid "%1$s by %2$s"
msgstr "%1$s von %2$s"

You can find more documentation on how to format your PO file here (It’s very simple, there is only 5 different declarations)

Or remove the context from your string like this

esc_html_x( '%1$s by %2$s', 'permalink title', 'themeberger' ); //With context
esc_html__( '%1$s by %2$s', 'themeberger' ); //Without context

You can find information on how to use the wordpress internationalization functions here

The documentation is pretty incomplete so I will add a list of the available functions

  • __ Basic translation
  • _n Translate a string with plural
  • _x Translate a string with a context
  • _nx Translate a string with a context and a plural

All of this function also have variants with esc_html_, esc_attr_ or _e in front of them

The _e variant will simply echo the string instead of returning it (I don’t recommend using this as sometimes you will want to sprintf in your string but if you use _e within a sprintf it will not work and it’s hard to catch)

The esc_html_ variant will escape the string to be displayed in html (eg: > becomes >) You should pretty much always use it in templates as it increases security by preventing XSS (some translations are public)

The esc_attr_ variant is to ALWAYS be used within an html attribute as it will escape quotes and others that would break your attribute

Example:

<a href="https://wordpress.stackexchange.com/questions/360209/<?php esc_attr_e("https://domain.com/en/content/', 'domain') ?>"><?php esc_html_e('My link', 'domain') ?></a>

You can also look at the source code to see all the declared functions