[wp-hackers] Language support for TinyMCE

Kirk Steffensen blogger at steffensenfamily.com
Thu Feb 16 18:27:42 GMT 2006


WP Hackers,

Below is a proposed change to wp-includes/js/tinymce/tiny_mce_gzip.php to
restore native TinyMCE language support.  Before I post this to Trac as an
enhancement, I wanted to make sure that I'm not missing something.  If no one
points out a logic error or a better way to build the mousetrap, I'll make a
patch and post it to Trac.

===BACKGROUND===

I was happy to finally find documentation on the TinyMCE filters at
http://codex.wordpress.org/Plugin_API#Rich_Editor_Filters - I even added some
example functions to the codex once I worked them out for the WPG2 plugin.

However, I haven't been able to figure out a way to restore the native TinyMCE
language support.  It looks like tiny_mce_gzip.php will apply
wp_translate_tinymce_lang, which in turn applies the overall WordPress mo and
po files.  So, if there is a phrase that's in the master mo and po, you're good
to go.  But since the TinyMCE plugins are not WordPress plugins, there is no
obvious way to make and apply a mo and po file for them.

The only language files that ship with the WP instance of TinyMCE are the en.js
files.  The only phrases that I could find supported in the overall WP mo and
po files are in support of the TinyMCE WP help popup.  If you want to install
the other lang files from TinyMCE, e.g., de.js or zh_TW.js, it appears you're
out of luck.

===PROPOSED CHANGE===

I propose changing the following code in tiny_mce_gzip.php to allow loading
TinyMCE lang files, if they exist.  The code change finds WP's locale setting
(xx), and then checks to see if there are corresponding xx.js files for core
TinyMCE, the active TinyMCE theme, and any plugins.  If there are not
corresponding xx.js files, then the default en.js file is loaded.  After that,
the the wp_translate_tinymce_lang function is still called.

Existing Code:
[code]
	// Load theme, language pack and theme language packs
	$theme = apply_filters('mce_theme', 'advanced');

	echo wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme .
"/editor_template.js")));

	echo wp_translate_tinymce_lang(file_get_contents(realpath("themes/" . $theme .
"/langs/en.js")));

	echo wp_translate_tinymce_lang(file_get_contents(realpath("langs/en.js")));

	// Load all plugins and their language packs
	$plugins = apply_filters('mce_plugins', array('wordpress', 'autosave',
'wphelp'));

	foreach ($plugins as $plugin) {
		$pluginFile = realpath("plugins/" . $plugin . "/editor_plugin.js");
		$languageFile = realpath("plugins/" . $plugin . "/langs/en.js");

[/code]

Proposed New Code:
[code]
	// Load theme, language pack and theme language packs
	$theme = apply_filters('mce_theme', 'advanced');

	echo wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme .
"/editor_template.js")));

	// Get the WordPress locale
	$locale = get_locale();

	$themeLanguageFile = realpath("themes/" . $theme . "/langs/" . $locale .
".js");

	if (!file_exists($themeLanguageFile))
		$themeLanguageFile = realpath("themes/" . $theme . "/langs/en.js");
	echo wp_translate_tinymce_lang(file_get_contents($themeLanguageFile));

	$tinymceLanguageFile = realpath("langs/" . $locale . ".js");

	if (!file_exists($tinymceLanguageFile))
		$tinymceLanguageFile = realpath("langs/en.js");
	echo wp_translate_tinymce_lang(file_get_contents($tinymceLanguageFile));

	// Load all plugins and their language packs
	$plugins = apply_filters('mce_plugins', array('wordpress', 'autosave',
'wphelp'));

	foreach ($plugins as $plugin) {
		$pluginFile = realpath("plugins/" . $plugin . "/editor_plugin.js");
		$languageFile = realpath("plugins/" . $plugin . "/langs/" . $locale . ".js");
		if (!file_exists($languageFile))
			$languageFile = realpath("plugins/" . $plugin . "/langs/en.js");
[/code]

We've tested this quite a bit in the WPG2 community and it works well.  Stephen
Ju of http://www.ju-ju.com/ did most of the development on this and has
successfully implemented a zh_TW.js file for my TinyMCE plugin
(http://g2image.steffensenfamily.com) and has restored zh_TW.js for the both
core TinyMCE and the advanced theme.

If we're missing an easier way to do this, or someone else has already hacked a
better hack, please let us know.  If not, I'll put together a patch and submit
it to Trac.

Thanks,
Kirk

Kirk Steffensen


More information about the wp-hackers mailing list