ExpressionEngine 2024 Quarter 1 Review Quarter 1 Review
So I’ve been bitten by the language switching bug and as such decided to create an EE based solution. That means you won’t have to parse segments or learn php to get this to work, it should be as simple as creating a few fields. Furthermore it should work with existing data, so if you’ve been waiting to go international it shouldn’t take much more than installing this.
That said, the configuration is done in the extension settings. Once there, you’ll see some helpful tips, but the long and short of it is that you’ll create a bunch of lines like this:
_ja:Japanese
What that means is if a user is browsing the site in japanese it should display fields with a ‘_ja’ suffix instead of no suffix (where available). So, if you have a field named ‘body’ and another field named ‘body_ja’ and the user is using Japanese the body_ja would get rendered instead of the body field.
Finally, to output the language switcher links simply place {exp:translator:simple} in your templates.
This extension also alters the search and profile pages by linking up with the built in EE language packs. All you need to do is make sure the title of your language has the same name as the folder of the language pack. So in the above example you would need a language pack named ‘Japanese’ in your /system/languages folder. Take note that case does matter, so if your title is Japanese then your folder should have a capital J. If it does not you should be able to rename the folder without too much trouble. (This will only work in PHP5 due to some inconsistencies in object references. The friendly folks at Ellis Lab have looked into a fix for this and when it is implemented I will make sure this extension takes advantage of that.)
To switch category groups you can use the same suffix as you do with weblog fields. So if you have a category group named ‘Blog’ and a user is browsing in Japanese it would use a category group named ‘Blog_ja’ instead of just ‘Blog’. In order for this yo work you have to place {exp:translator:simple category_group="4"}
wherever you would normally place the category group. So if the ‘Blog’ category group had an id of 4 and you wanted to switch it out in a weblog:categories tag, you could do that like so:
{exp:weblog:categories weblog="blog" category_group="{exp:translator:simple category_group="4"}"}…{/exp:weblog:categories}
The same also now works for switching entire weblogs:
{exp:weblog:entries weblog="{exp:translator:simple weblog="blog"}"}…{/exp:weblog:entries}
The category_group and weblog MUST be surrounded with double quotes, single quotes WILL NOT WORK.
That’s it, it should be pretty simple, but of course, let me know if it’s not.
Hi Mark,
tried to play with the extension, but I get the following error when accessing the extension manager:
Parse error: parse error, unexpected T_OBJECT_OPERATOR in /.../extensions/ext.language_switcher.php on line 198
Both files seem to be in the correct place and I already tried a new upload. Any ideas?
-Markus
Works great, Mark. Might be very useful at some time. Really appreciate all your work!
But I have two questions regarding usage:
Is it possible to change the title as well? I’ve added a field named title_de, but that doesn’t seem to work.
Any possibility to check which language is selected from inside the template? Seems the switch isn’t done via a URL segment.
Hi Mark. This looks nice. Thanks for posting it. For a minute there I thought you had saved me a bunch of time, but, alas, I need category (sub navigation) and main navigation to also change when the language is switched, so i’ll still need to go custom. 😊 However, I’m interested in talking to you about expanding this to something that covers categories, members etc. EE is in need of a robust solution for languages.
well ryan, the biggest problem, is finding a place to store the translated content. we could store it in the category description but then that renders the description useless for it’s intended purpose. the only other thing I can come up with is to create a global “translation key” where you can define translations to occur on a word-to-word basis. then if any of the “keyed” words show up we can translate them based on the key…
True, Mark. I’m definitely not into polluting other useful content fields. The translation key might be a possiblility, but I could see that getting very complex quickly.
In a very abstract sense, I’m thinking about just relating category groups. One group for English, one for Japanese, etc. How you would translate that into a workable and usable extension is another question entirely! 😉
Mark how does the extension know the user is “browsing japanese”? The extension is checking something with the browser - what exactly? Just curious. Thanks.
What that means is if a user is browsing the site in japanese it should display fields with a ‘_ja’ suffix instead of no suffix (where available).
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.