4

I've been looking at How can I build a language switcher? but I don't to hit the DB for each entry in the loop, just to get the entry's localised slug.

However I can't see a way to get an entry by ID with anything other than the current or a specific locale.

So assuming I'm on a template where we have an entry defined, this returns 1 entry in current locale:

craft.entries.id(entry.id);

And this returns 1 entry in a different locale:

craft.entries.id(entry.id).locale('es');

How can I get entries for ALL locales so I can do one DB query rather than one for every locale that's enabled on the site?

2

I don't think this is possible witout the use of a plugin. The craft.entries tag works on an entry basis. The slug is saved in the table that houses the localized data for an element, craft_elements_i18n. When you request an entry, (in theory) the craft_entries table is joined with the craft_elements table (based on id), which is then joined with the craft_elements_i18n table (based on locale). There's no way you could join in data from several different locales this way.

What you have to do is query the craft_elements_i18n directly, and return all rows based on the entry id (elementId in that table is the entry id), and then you'd get locale, slug, and url. This table isn't exposed in any way to the templates, so you'd need to do it with a small plugin. It will still be one query to do this, though.

I think maybe this is more work than what it's worth, unless you have loads of locales, the difference in performance will be very, very small. Also, you could cache this in your templates, which would make it even less of an issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.