Handling Translations via Panel and publishing single language?

Hi there,

i am setting up a new blueprint for my Kirby-Projects and migrate my functionalities from Kirby v2 to v3. So i came up with some questions:

In other CMS like Drupal or so there is something like a taxonomy. Like a Page where you would enter all language variables in every language, which you could then access via Template. That would be very neat, because you wouldn’t have to enter them in the language variable files. But i have seen, that you now could access language variables via YAML files, so could this be possible??

And the second question is: is it possible to only publish one language of a page? I had this usecase quite often when most content is in German and only some of them had translations in let’s say English or Chinese. For v2 i had @flokosiol 's translations plugin, which doesn’t seem to work under v3 anymore. Are there any other approaches you could apply for such a usecase? Just defining a bool for publishing would not be so nice…

Thanks in advance so far.
Greets Tobi

Hey Tobi, there’s a draft of the translations plugin for K3 in the Kirby3 Branch. It’s far away from being perfect, but it might help you nevertheless?!

Greetings from Mainz.
Hope you‘re doing fine :wink:

Hey Flo, yeah so far so good. Everything is working out at the moment.

Okay i’ll have a look at the kirby3 branch. Are there any known issues that could affect production? Because you wrote it in the Description?

Unfortunately i get errors with the first test. What is the current state of the Plugin, you’re going to work on it in foreseeable future? Maybe you could tell me what the problems are and i try it by myself and give you a pull request?

Check out Trevor:

Or as an alternative:

Thank you so far. How can i access them in the template?

I just had a look at the first one and it seems it stores the variables in the language files. So you can fetch them like normal using the t() helper.

Okay thank you. But i have to keep the variables in sync with the template. So if i have a variable let’s say ‘test.variable’ and i say <?= t('test.variable') ?> it gets not updated when i’d change the variable key in the panel, right? Is there a possibility for retrieving all translations-entries and filter them?

Anyway. This is of great help. Thank you so far. I’ll define the use-case tomorrow and have a look how it will work out…

Yes, see:

Filter them by what?

In some place you will have define what sort of variable is used, let’s say you want a button in page. In a field for a button, the user would insert a language variable (button.text). Then in Trevor view, the user would enter the different translations for this variable. You would then get the name of the variable form the the field (or maybe from a custom button kirbytags or whatever.

Okay yes. Thanks i’ll test that.

I think the most common use-case would be the name for a Company. Which could be Test GmbH&Co.KG or whatever and another legal state in English. So instead of keeping track of every single use in the CMS it would be helpful to define them in the variables, use them in templates and even as KirbyTags for the Textareas. So you’d just say <?= t('company.name') ?> or { company.name } in textareas or whatever. But i think it could be possible with the possibilities i now have on hand. I’ll keep you posted. Thanks in advance.

For using it in textareas or other fields, you could render those with KirbyText and use a hook to replace your {company.name} placeholder with the value you want:

@distantnative It seems that a dot in a placeholder doesn’t play with the Str::template() method.

Example:

    'translations' => [
        'company.name' => 'Supercomp Ltd.'
    ],

In text file:

{{ company.name }} is the best company in the world.

In hook:

    'hooks' => [
        'kirbytags:after' => function ($text, $data, $options) {
            $translations = $this->language()->translations();
            return Str::template($text, $translations);
        }
    ]

The code works fine without dots in the keys. Bug? Something wrong in the code?

Just checked: Not really a bug, but the dot is used as an identifier whether the query language is used or not: https://github.com/getkirby/kirby/blob/master/src/Toolkit/Str.php#L893-L895

Ah, ok, thanks, so that means we can’t use Str::template() when using dots in placeholder keys which is quite common. Anyway, this works using the code from the template() method without the query language check:

    'hooks' => [
        'kirbytext:after' => function ($text) {
            $translations = $this->language()->translations();
            return preg_replace_callback('!' . '{{' . '(.*?)' . '}}' . '!', function ($match) use ($text,$translations) {
                $query = trim($match[1]);
                
                return $translations[$query] ?? $query;
            }, $text);
        }
    ]

I might consider this still a bug - the current state feels a bit too opinionated.
And I think we could change that one line to

return (new Query($match[1], $data))->result() ?? $data[$query] ?? $fallback;

and then it should work

1 Like