Minimalistic content translation in WordPress.
Minimalistic content translation in WordPress. The plugin handles translations for posts, postmeta, options, siteoptions (multisite) and usermeta., (*1)
While the plugin also handles comments and commentmeta they are not translated. Comments are mapped to a specific language (the langauge active when posting the comment) and will be displayed for that specific language. There's also an option to make languages inherit comments from other languages. Forinstance the nordic languages is very similar, so we want to load Danish and Norwegian comments along with the Swedish comments., (*2)
The GUI is is available in:, (*3)
If you are using redis or memcached, you want to define a hash-key depending on selected language. The hash key cannot be changed at runtime after it has been set (defined as constant). Add this to your configuration:, (*4)
if (isset($_COOKIE['wp_content_translator_language']) && !empty($_COOKIE['wp_content_translator_language'])) { define('WP_CACHE_KEY_SALT', NONCE_KEY.$_COOKIE['wp_content_translator_language']); } else { define('WP_CACHE_KEY_SALT', NONCE_KEY); }
You can easily use the default language selector (a basic html <select>
element) or create a language selector with custom markup., (*5)
Use the wp_content_translator_language_selector()
function to display a language selector. If you want to use the default selector just run the function without any paramters. If you want to create a custom selector there's an option to pass markup for the wrapper and the "language rows"., (*6)
Available template tags to use in the markup:, (*7)
Wrapper:, (*8)
Language row:, (*9)
wp_content_translator_language_selector( $wrapper = '<ul>{{ languages }}</ul>', $element = '<li><a href="{{ url }}" class="{{ isCurrent }}">{{ name }}</a></li>' )
Each translation component has it's own configuration filter which can be used to change it's configuration., (*10)
function my_custom_config($config) { $config['key'] = 'my value'; return $config; } add_filter('wp-content-translator/configuration/{{component}}', 'my_custom_config');
Available configurations: - bool translate_fallback - Fallback to default language or not - string translate_delimeter - Delimeter to use, (*11)
Available configurations: - bool translate - Use the component or not, (*12)
Available configurations: - bool translate - Use the component or not - bool translate_hidden - Translate options prefixed with underscore or not - bool translate_numeric - Translate numeric values or not - array untranslatable - Array with option keys of untranslatable options - array translatable - Array with option keys of translatable options, (*13)
Decide if the plugin should download the WP language pack when installing languages., (*14)
@param string $answer
- The default answer@param string $code
- The language code@param \ContentTranslator\Language $language
- The language objectfunction my_download_wp_translation(bool $answer, string $code, \ContentTranslator\Language $language) { if ($code === 'sv_SE') { return false; } return $answer; } add_filter('wp-content-translator/should_download_wp_translation_when_installing', 'my_download_wp_translation', 10, 3);
Filters the name of the current language in the admin bar., (*15)
@param string $language
- The name of the language@param string $code
- The language codefunction my_admin_bar_current_lang(string $language, string $code) { if ($code === 'sv_SE') { return 'Skånska'; } return $language; } add_filter('wp-content-translator/admin_bar/current_lang', 'my_admin_bar_current_lang', 10, 2);
Where to redirect to after a langauge have been uninstalled., (*16)
@param string $url
- The default redirect url@param string $code
- The language code@param \ContentTranslator\Language $language
- The language objectfunction my_after_uninstall_redirect(string $url, string $code, \ContentTranslator\Language $language) { return 'http://www.helsingborg.se'; } add_filter('wp-content-translator/redirect_after_uninstall_language', 'my_after_uninstall_redirect', 10, 3);
Set up inheritance for comments. Load comments from multiple languages for a language. Example: Load Swedish, Norwegian and Danish comments if the current language is Swedish., (*17)
@param array $connections
- The default connections@param string $code
- The language codefunction my_comment_connections(array $connections, string $code) { return 'http://www.helsingborg.se'; } add_filter('wp-content-translator/comment/connections', 'my_comment_connections', 10, 2);
Is the meta type translate component installed or not?, (*18)
Available components: post, comment, option, siteoption, meta (postmeta), user (usermeta), comment (commentmeta), (*19)
@param bool $isInstalled
- The default is installed value@param string $code
- The langauge codefunction my_is_usermeta_installed(bool $isInstalled, string $code) { if ($code === 'sv_SE') { return true; } return $isInstalled; } add_filter('wp-content-translator/user/is_installed', 'my_is_usermeta_installed', 10, 2);
Whether to remove metadata when uninstalling the translation component., (*20)
Available components: post, comment, option, siteoption, meta (postmeta), user (usermeta), comment (commentmeta), (*21)
@param bool $shouldRemove
- The default should remove value@param string $code
- The langauge codefunction my_should_remove_meta(bool $shouldRemove, string $code) { if ($code === 'sv_SE') { return true; } return $shouldRemove; } add_filter('wp-content-translator/user/remove_meta_when_uninstalling_language', 'my_should_remove_meta', 10, 2);
Default return value for shouldTranslate
method, (*22)
Available components: post, comment, option, siteoption, meta (postmeta), user (usermeta), comment (commentmeta), (*23)
@param bool $shouldTranslate
- The default should translate value@param string $code
- The langauge codefunction my_should_translate_default(bool $shouldTranslate, string $code) { if ($code === 'sv_SE') { return true; } return $shouldTranslate; } add_filter('wp-content-translator/user/should_translate_default', 'my_should_translate_default', 10, 2);
Runs before a language is installed., (*24)
@param string $code
- The langauge code@param \ContentTranslator\Language $language
- The langauge objectfunction my_before_install_language($code, $language) { // Do my stuff } add_action('wp-content-translator/before_install_language', 'my_before_install_language', 10, 2);
Runs after a language have been installed., (*25)
@param string $code
- The langauge code@param \ContentTranslator\Language $language
- The langauge objectfunction my_after_install_language($code, $language) { // Do my stuff } add_action('wp-content-translator/after_install_language', 'my_after_install_language', 10, 2);
Runs before a language is uninstalled., (*26)
@param string $code
- The langauge code@param \ContentTranslator\Language $language
- The langauge objectfunction my_before_uninstall_language($code, $language) { // Do my stuff } add_action('wp-content-translator/before_uninstall_language', 'my_before_uninstall_language', 10, 2);
Runs after a language is uninstalled., (*27)
@param string $code
- The langauge code@param \ContentTranslator\Language $language
- The langauge objectfunction my_after_uninstall_language($code, $language) { // Do my stuff } add_action('wp-content-translator/after_uninstall_language', 'my_after_uninstall_language', 10, 2);
Runs before language switcher is added to the admin bar., (*28)
function my_admin_bar_before() { // Do my stuff } add_action('wp-content-translator/admin_bar/before_add_switcher', 'my_admin_bar_before', 10);
Runs after language switcher have been added to the admin bar., (*29)
function my_admin_bar_after() { // Do my stuff } add_action('wp-content-translator/admin_bar/after_add_switcher', 'my_admin_bar_before', 10);
Runs right before the "language" options page is added to the admin menu., (*30)
function my_options_page_before() { // Do my stuff } add_action('wp-content-translator/options/before_add_options_page', 'my_options_page_before', 10);
Runs right after the "language" options page have been added to the admin menu., (*31)
function my_options_page_after() { // Do my stuff } add_action('wp-content-translator/options/after_add_options_page', 'my_options_page_after', 10);
Runs when installing a translation component., (*32)
Available components: post, comment, option, siteoption, meta (postmeta), user (usermeta), comment (commentmeta), (*33)
@param string $code
- The langauge codefunction my_user_meta_install(string $code) { // Do my stuff } add_action('wp-content-translator/user/install', 'my_user_meta_install', 10);
Runs when uninstalling a meta type translate component., (*34)
Available components: post, comment, option, siteoption, meta (postmeta), user (usermeta), comment (commentmeta), (*35)
@param string $code
- The langauge codefunction my_user_meta_install(string $code) { // Do my stuff } add_action('wp-content-translator/user/uninstall', 'my_user_meta_uninstall', 10);