Sublanguage is a lightweight multilanguage plugin for wordpress.
Sublanguage is a multilanguage plugin for wordpress.
In version 2.9, a security vulnerability (medium severity) was reported for Sublanguage. In order to ease the fixing, we chose to permanently remove a few under-used features concerned by this exploit in v 2.10. Please write in the forum if you disagree with this choice.
Plugin documentation is available on github
Upload and activate the plugin
Add languages
Add a language switch on the site (within a menu)
Add a language switch in a widgets zone
Add a language switch anywhere on the site (for developpers)
do_action('sublanguage_print_language_switch');
. You can customize HTML output through the filter “sublanguage_custom_switch” (read FAQ for more info).Add translatable custom post types or custom taxonomies
Plugins or themes may add any number of extra post-type and extra taxonomies. If you can’t figure wether a post type or taxonomy need to be translatable, please ask in the forum.
Translate menu items when your menu is using custom links or item names different from page titles
Add or edit language screen.
Post.php screen with language switch and tinyMCE button for quick interface.
Tinymce plugin: quick interface for translation
Wp.media interface with language tabs for medias translation
Edit-tags.php screen.
Nav-menus.php screen with language custom metabox
Options-permalink.php screen with inputs for taxonomy slug or custom post archive slug translation.
Minimal UI settings
In menu click on Languages
, remove all language custom posts and empty trash. Deleting a language will permanently delete all translations
associated to this language. Deleting main language will not delete original posts.
Add this function in your template file
do_action('sublanguage_print_language_switch');
Add this in your function.php
file and customize it:
add_action('sublanguage_custom_switch', 'my_custom_switch', 10, 2); /** * @param array of WP_Post language custom post * @param Sublanguage_site $this The Sublanguage instance. */ function my_custom_switch($languages, $sublanguage) { ?> <ul> <?php foreach ($languages as $language) { ?> <li class="<?php echo $language->post_name; ?> <?php if ($sublanguage->current_language->ID == $language->ID) echo 'current'; ?>"> <a href="<?php echo $sublanguage->get_translation_link($language); ?>"><?php echo $language->post_title; ?></a> </li> <?php } ?> </ul> <?php }
If you are using menus and you want the language switch into a menu,
go to Display > Menu, open option drawer and verify ‘language’ is selected.
Then add as much ‘language item’ as you have languages.
You can even distribute languages on different hierarchy level.
If you need current language to be on the first level and further language on the second, you will also want to check current language first
in Settings -> Sublanguage
First go to Sublanguage>Settings
and verify the relevant post type is set to be translatable.
Then verify you are using the proper filters in you template file.
For echoing post title, you need to ensure ´the_title´ filter is called.
// echoing post title inside the loop the_title(); // echoing post title outside the loop echo get_the_title($some_post->ID); // but... echo $post->post_title; // -> Does not translate
For echoing post content, you need to ensure ´the_content´ filter is called.
// echoing content inside the loop the_content(); // or... echo apply_filters('the_content', get_the_content()); // but... echo $post->post_content; // -> Does not translate echo get_the_content(); // -> Does not translate // echoing post content outside the loop: echo apply_filters('sublanguage_translate_post_field', $some_post->post_content, $some_post, 'post_content');
Same for Excerpts.
Permalinks are automatically translated, inside or outside the loop:
// echoing permalink echo get_permalink($post->ID);
In your template files, verify texts are properly localized, and language packages are properly installed.
Use the global $sublanguage
, like this:
global $sublanguage; echo $sublanguage->current_language // -> WP_Post object echo $sublanguage->current_language->post_title; // -> Français echo $sublanguage->current_language->post_name; // -> fr echo $sublanguage->current_language->post_content; // -> fr_FR
Alternatively you can use a sublanguage filter to call a user function with $current_language
value in parameters:
Function to use in your template file:
echo apply_filters('sublanguage_custom_translate', 'text to translate', 'my_custom_translation', 'optional value');
Code to add in your function.php
file:
/** * @param string $original_text. Original text to translate. * @param WP_Post object $current_language * @param mixed $args. Optional arguments */ function my_custom_translation($original_text, $current_language, $optional_arg) { if ($current_language->post_name == 'fr') { return 'texte traduit en français!'; } return $original_text; }
Note: of course, for a basic usage like this, you should use the standard localization way: __('text to translate', 'my_domain')
.
Go to Sublanguage>Translate Options
and try to find the corresponding option key. Options may be nested in a data tree.
Go to Sublanguage>Translate Options
and find the corresponding widget option name (like ‘widget_somthing’). Expand items with value corresponding to ‘DATA’ until you find the text you need to translate.
Your nav menu items that are linked to translated posts, pages or terms should be automatically translated.
If you need to translate custom link or to change the default value for items name, you can select “Nav Menu Item” in “Translate post types” section of Sublanguage settings.
Then open Sublanguage>Nav Menu Item
and edit like a normal post.
Go to Sublanguage settings and select custom post-meta key in the checkbox list under “Translate Meta”. A meta key needs to be at least used once to appear in this list.
Go to Sublanguage settings and select ‘_thumbnail_id’ in the checkbox list under “Translate Meta”. At least one thumbnail must be set before metakey appears in this list.
Add this action to enqueue a small script to define values in javascript:
add_action('init', 'my_init'); function my_init() { do_action('sublanguage_prepare_ajax'); }
This will first define a global in javascript. Use console.log(sublanguage)
to explore it.
Furthermore, a small script will automatically add a language attribute in every jquery ajax call. You can change this language using sublanguage.current
(in javascript). This language will be used if you need to get/update posts/terms using ajax.
You cannot export or import using the wordpress builtin tool while Sublanguage is active. It just does not work yet. But this feature will come in a future release.
If you want to create a custom importer for posts and terms, you can use these 2 functions:
do_action( 'sublanguage_import_post', $data); do_action( 'sublanguage_import_term', $taxonomy, $data);
These functions are documented in sublanguage/include/admin.php. See examples on github.
Yes it will, unfortunately. A few more database queries are necessary to load every page.
If performance drops noticeably, you may want to install a cache plugin. Sublanguage have been sucessfully tested with WP Super Cache and W3 Total Cache (with default settings).
Sublanguage also works with SQLite Integration plugin.
Use any language instead, then update, then edit language title, slug and locale, then update again.
Sublanguage_site::get_default_language_switch
functionwp_term_taxonomy
table).'home_url'
except in post.php
page, in order to prevent possible bugs when rebuilding permalinkscontext
parameter for sublanguage_print_language_switch
and sublanguage_custom_switch
hooksload_plugin_textdomain
now only called in admin.sublanguage_current_language
. Use sublanguage_init
instead.sublanguage_load_admin
. No alternative.sublanguage_translate_term_field
to allow translation in any languagesublanguage_translate_post_field
to allow translation in any languagesublanguage_enqueue_terms
action to handle custom translation terms querysublanguage_enqueue_posts
action to handle custom translation posts querytranslate_post_type_archive_link()
function did not return the correct link for main language if it was edited.sublanguage_custom_switch
hook with only one language was causing errorwp-admin/post.php
triggered ajax of all submit buttons, including Delete
in Custom Fields
box, which deleted all post meta.Tags
box on wp-admin/post.php
were not translatedregistration_redirect
function. Language was lost after registering.translate_login_url
. Language was lost on login screen when english was not the main language.sublanguage_translate_post_field
. Filter was not called in admin.Some changes in readme file and adding medias (screenshots, banner, etc.).
Undocumented modifications.
Undocumented modifications.