Hi, I’m using a multilang install of kirby, with french (default) and english. Since Kirby creates both language txt files, I wanted to create a Translated ? field and show it in the panel only in english, using conditionnal fields. I’m stuck at the query that gets the current language as I don’t know how to query it properly and how to store it.
I was hoping I could do something like this, putting the value in text field when creating a new page:
fields:
isTranslated:
type: toggle
when:
currentlang: en
currentlang:
type: text
disabled: true
default: "{{kirby.language}}"
But once created, the value is {{kirby.language}}
. Any idea?
Thanks!
Currently, you can’t to use queries for default values (at least not that I know unless I’ve missed something).
I tried something else yesterday before posting what was I though a simple syntax/request problem:
currentlang:
label: langs
type: radio
options: query
query:
fetch: kirby.languages
text: " {{kirby.language}}"
value: " {{kirby.language}}"
And it’s working, fetching the array of languages and displaying the current one correctly:
I’m not sure if it’s an intended behaviour.
Probably not with two identical entries?
Yeah that’s not what I wanted but I was trying for the first time the query syntax and came up with this. Of course what I need is a single text entry for the current language and that’s all but I can’t nail the query or yml syntax to use.
I found a better way:
currentlang:
type: select
options: query
required: true
query:
fetch: kirby.languages.filterBy("code", kirby.languages.current.code )
text: "{{ kirby.language.name }}"
value: "{{ kirby.language.code }}"
Unfortunately, it’s not possible to use a default as query, but at least this gives you only one option to select from and since it’s also required…
This PR already contains queries for more field props, unfortunately not the default
property.
Thanks for your help @texnixe, I’ll see what I can do with this. And thanks for asking in the PR for me, I’ll post there too. (Also thanks for answering me at 22pm on Sunday but you know, I could have waited monday after your first coffee/tea )
1 Like
We don’t have fixed working hours and work when we want and have the time (i.e. often outside of our 9-5 day job schedules).
1 Like
Another day, a hopefully better idea. Why not store the current language in a hidden/disabled field at page creation instead of using the `currentlang" select?
That’s what I initially intended to do, using the default in blueprint to get and write the language, then hide or disable it.
currentlang:
type: text
disabled: true
default: "{{kirby.language}}"
I focused on doing it into the yml file as I was working on it, and didn’t though about another way of achieving this. Are you thinking about another way?
Yes, as I wrote above, write the value to file on page creation, i.e. using a page.create:after
hook.
And it kinda works! First time trying hooks:
'hooks' => [
'page.create:after' => function ($page) {
$lang = $this->language()->code();
$page->update([ 'currentLang' => $lang ], $lang);
},
'page.update:after' => function ($page) {
$lang = $this->language()->code();
$page->update([ 'currentLang' => $lang ], $lang);
}
]
With this when a page is created, the currentLang field is filled with the lang code. Switch language, make a change in any field and save and it updates the new language too. Maybe there’s a better way of doing it?
Yes, in your page create after hook, loop through all languages and update them all
'page.create:after' => function ($page) {
foreach (kirby()->languages() as $lang) {
$code = $lang->code();
$page->update([ 'currentLang' => $code ], $code);
}
},
This should add the language code to each translation file. This probably has the downside of no longer showing the original text of the default language to be translated when switching languages in the Panel, because the translation now physically exists, but haven’t tested this.
Yes it’s workin as intended and as you guessed, it does not import content when translating. I don’t have that much things to translate to be honest so that works for me. Thanks again for your help.
Fine that it will work for you. I think in the future, we will have better options when queries come to defaults.
Or if we had more options for conditional fields like a not
option, then it would be enough to only update the default language using a hook.
Well, for the time being we have to make the best of what we have. At least there’s almost always some kind of workaround if the straightforward solution doesn’t exist yet :kirby:.
(Hm, our Kirby emoji is quite blurry )