The goal is to offer a button/select drop-down or similar to switch the public interface1) language on the fly.
What I’m searching for
- Plugin or Theme Code…
- … or Ideas
- Using a work-around with
load_textdomain()
would be appreciated to leave translation to .po/.mo files - Ideas on how to parse the strings into the interface (ex. ajax/plain php/ini, json, xml files) when not relying on some textdomain function
Note:
1) It’s not about publishing in different languages.
2) I don’t need code to make the actual drop-down/button/whatever. It’s only about the code/system delivering the strings for the UI.
Thanks!
By far the best (easiest) way is to use the locale filter (inside
get_locale()
).First set up a quick function for retrieving a different language to use on the
locale
filter.I’ve left this as a global function, might be handy…? The rest of the plug-in I’ve wrapped inside a class.
It creates a drop-down of possible languages from which you can select.
Drawbacks
wp-content/languages
for files of the formxx_xx.mo
. If it’s not of that form (not all .mo files are) the plug-in won’t pick up on it!$locale
do anything other than language? If so it’s still possible to get the locale the same, but provide alternative translations. It’s a lot messier though…The code
If you can settle for a page refresh, redefining the
WPLANG
constant could be an option. I’m doing that on two sites with multilingual content where the multilinguality plugin fails to trigger UI translation.http://www.qianqin.de/qtranslate/
is what you need …
Edit I – after comment.
First – thanks for all who contributed in the downvotes torrent.
(this is what happens when one does not visit frequently enough :- ) )
now –
The function that handles the switch is in qtranslate-core.
(starts at line 80 more or less – depends on the version that you want.)
Since you can not look into a NOT OOP code , and I am now with nothing else to do – I took 10 minutes to synthesize , paraphrase compile that non-OOp with yet another non-OOP code for you .
(sorry, – primitive old skool here)
Assuming that I understood the question && Assuming you want the code to be for admin && assuming you will know how to port it to front end if you want && assuming you understand that the code is not optimal :
REMARKS :
1 – This was built in 10 min. on local machine while at the airport .
The reason why I say this is that the only ready available wordpress installation that I have here is quite old . (2.9 I think).
This means that probably the language menu will appear UNDER the contextual help div, due to the fact that a new div structure was recently introduced (with tabs) – but still it should work.
(I promise to get back to this later if I have time)
If I had more time (and a new wordpress code here ) i would probably integrate this in the new admin-bar with
add_action( 'admin_bar_menu', 'k99_add_language_menu_2', 1000 );
(I already promised to get back to this)2 – you should have the listed languages files inside the “languages” folder in wp-content (better??) or wp-include.
regarding this point – the code is only a proof of concept – and of course if one has more time the menu should be built in a dynamic way and not like done here.
3 – your WPLANG in config should be empty. I am not sure it will work on newer wp versions if a language is already set.
4 – it is a simple synthesized and paraphrased proof of concept built on what was seen in qtranslate code (also old version) – so most of the things are done the “quick” way, not necessarily the “right” way (like adding JS, no dedicated textdomain etc..).
EDIT II
someone is lucky today ! (Flight Delay )
Since you were looking for “ideas” for approaches , here is another famous one .
It will not fit in a plugin, or actually any other non-exclusive environment , but it might give you another direction as how to handle the “Browser stuff”.
This will allow one to get the language with a GET like :
<a href="index.php?lang=de">German</a>
or<a href="whatever.php?lang=ml">my Language</a>
To use , you can create a code (let´s call it wp-langswitch.php )
now, in wp-config, just before the WPLANG constant , we need to include our code .
Now – obviously it would be better to add some cookies to the mix to store the languages .
EDIT III
I promised to get back to this later – here is how (in a very non-correct and primitive way ) it could be integrated inside the admin-bar (instead of the contextual-help-menu)
Just replace this function (or add) to the old code (the original NON – OOP non-plugin) .
Like I said, I do not have a new wordpress installation here on local that features admin-bar – but it should work .
(and then again, it might not work…but I am sure you could fix that if you really want – even thought it is not OOP ) 🙂
gotta run now . hope it helps somehow.
EDIT IV – changed edit III to working verion for admin menu bar (at least on my wp 3.4)
since I had no working install of 3.4 – and now I returned and I do – I have edited the Edit III solution – and it works for me .
About the upvotes, I could not care less . And I do not need any “bounties” (whatever those may be..) It was just for the sake of the exercise you shlould maybe give it to the author of qTranslate . 🙂 despite of the fact that it is not OOP – it is a genious plugin with a lot to learn from in a lot of areas . considering the “when” of this plugin release , it is even amazing. worth the indescribably effort of reading a NON OOP code – even if it is not – OOP . And even if it is not correctly indented (good god !).