How to get a multilanguage string processed
-
I am a plugin developer and i want to know what i have to do to get a multilanguage string translated.
Example: My string is:
$str = '{:en}This is a dog{:}{:nl}Dit is een hond{:}';I expect (parallel to how qTranslate-x works) that:
echo __( $str );prints the string in the currently selected language, but it prints the string raw as it is.What do i have to do to get this working?
-
To get string in current language
$str = '{:en}This is a dog{:}{:nl}Dit is een hond{:}'; echo apply_filters('the_title', $str );Thanx for the quick reply, but this has unwanted side-effects, like stripping tags as soon as wpglobus is activated.
Is there not simply an api function e.g.wpglobus_translate_string( $str )or the like?-
This reply was modified 7 years, 6 months ago by
Jacob N. Breetvelt.
see wp-content\plugins\wpglobus\includes\class-wpglobus-core.php
WPGlobus_Core::text_filter()-
This reply was modified 7 years, 6 months ago by
Alex Gor.
This is my string:
$desc = '<span style="color:red" >{:en}Birds{:}{:nl}Vogels{:}</span>';Without wpGlobus activated it prints:
{:en}Birds{:}{:nl}Vogels{:}in red.With wpGlobus activated, the string passes this code:
// wpGlobus global $wppa_lang; // Holds the current language code ( nl or en ) if ( class_exists( 'WPGlobus_Core' ) ) { $desc = WPGlobus_Core::text_filter( $desc, $wppa_lang ); }When the language on the menu is set to en it prints:
Birds, when set to nl it printsVogelsin gray, the default color.Inspecting the page source learns that the
<span>tag is stripped.This makes it impossible for my users to enter multilangage text on places when html is allowed.
A second issue is that in the followng example the Lion eats the Cat:
{:en}Lion{:}{:nl}Leeuw{:}{:en}Cat{:}{:nl}Kat{:}An example what i want to be possible:
<table><tbody> <tr><td>{:en}Lion{:}{:nl}Leeuw{:}</td><td>7 {:en}years{:}{:nl}jaar{:}</td></tr> <tr><td>{:en}Cat{:}{:nl}Kat{:}</td><td>5 {:en}years{:}{:nl}jaar{:}</td></tr> </tbody></table>It looks like text outside a {:xx}{:} pair is ignored as well as any subsequent {:xx}{:} pair of the same language.
Can you fix this?
This should work:
$birds = apply_filters( 'the_title', '{:en}Birds{:}{:nl}Vogels{:}' ); $desc = '<span style="color:red" >' . $birds . '</span>';Also, you could put all those texts, with markups, to private pages and insert the content of those pages in your code. This way, you could edit the pages in multiple languages using the WPGlobus interface.
-
This reply was modified 7 years, 6 months ago by
TIV.NET INC..
Yes, but my users must have the ability to enter the html and the multilanguage, but no php code.
using qtranslate-x, the following text:
<span style="color:red" >{:en}Lion{:}{:nl}Leeuw{:}{:en}Cat{:}{:nl}Kat{:}</span><br /> <span style="color:red" >[:en]Lion[:][:nl]Leeuw[:][:en]Cat[:][:nl]Kat[:]</span>shows:
LionCat LionCator:
LeeuwKat LeeuwKatin red.
with wpGlobus, i get
Lionor:
Leeuwin gray.
So, qTranslate-x even supports {:xx}{:} tags (!)
Multiple pages is no option. My plugin (wp-photo-album-plus) really needs a db friendly in-line multilanguage solution. I can not have multiple – language dependant – db tables describing all metadata of photos, videos etc. This would lead to chaos and asynchronicity.
There is an “unofficial” method that you can try:
If still not working for you, you are welcome to suggest a solution and submit a PR to our repository’s “develop” branch.
Yes,
WPGlobus_Core::extract_text()does the trick, and, to my opinion, this is how it should work. Is this future safe or do i have to test on the existence of extract_text() ?The method will stay, but we do not claim it will work in all possible combinations of markup and multi-lingual texts.
I can live with that. I can now make wp-photo-album-plus compatible with WPGlobus, provided that they do NOT translate the wp options, and are willing to manually enter the {:xx}{:} tags.
My solution is as simple as can be:
// Do translation for wpGlobus function wppa_translate( $text = '' ) { global $wppa_lang; if ( class_exists( 'WPGlobus_Core' ) ) { $text = WPGlobus_Core::extract_text( $text, $wppa_lang ); } return $text; } add_filter( 'gettext', 'wppa_translate' );Thank for your help. case closed.
Filtering gettext might slow down the entire site. It’s called too many times.
1. I’d add such filter only if WPGlobus is active.
2. And I’d not apply it on gettext, but on a place in your code where you deal with your specific texts. Not the entire WP translations!Ok, changed it inot:
// Do translation for wpGlobus function wppa_translate( $text = '' ) { global $wppa_lang; if ( strpos( $text, '{:' ) !== false ) { $text = WPGlobus_Core::extract_text( $text, $wppa_lang ); } return $text; } function wppa_filter_wpglobus() { if ( class_exists( 'WPGlobus_Core' ) ) { add_filter( 'gettext', 'wppa_translate' ); } } add_action( 'plugins_loaded', 'wppa_filter_wpglobus' );There are much too many places of __() _e() to replace them by different named functions, so i think this comprimise should be acceptable.
-
This reply was modified 7 years, 6 months ago by
The topic ‘How to get a multilanguage string processed’ is closed to new replies.