Hello. I’m currently working on a multilingual kirby website and would like to serve different images based on language. I am a bit at a loss on how to go about it so any help would be greatly appreciated. Thanks in advance.
I did that once for a project by filtering the images via filename, each image started with a language code followed by an underscore (de_image.png, en_image.png); an alternative would be to filter via image meta data. Come to think of it, you could also use subfolders for images on a per language basis.
Sorry if i’m asking the obvious but how would I detect language through the template?
$site->language()->code()
outputs the code of the current language.
Then you can do something like this:
$lang = $site->language()->code();
$images = $page->images()->filterBy('filename', '*=', $lang . '_');
Thanks! That’s perfect!
You could also use a image field (as a select, with the Selector plugin or the new Kirby Image Field coming with 2.3) and change the field’s content for every language. For a small site, this should be more flexible and faster than using hard-coded names (or doing a search/replace function to always get the right language’s file). But for more complex sites, I did it the way @texnixe recommended – although it was a main wish by the client – the image names define its function directly, without choosing the image again.
That sounds very interesting although to be honest I don’t really get how you would go about changing the content of the image field. Could you elaborate please?
You can drag and drop (or select) an image into the image field. You can do that for each language. The downside is that you somehow have to identify the images you want in a page. So if you do this in this manual way - depending on your use case - you might end up with very long lists of images to choose from.
The best solution depends on what exactly you want to achieve. After all, you can of course, drag your images into a textarea field as well on a per language basis.
Yes, I get it now. The image field content is stored in the specific language text file. I think this would be the optimal solution. Thanks.