wordpress - WPML get translated content of a string from all languages if available -
using wpml, have translated string in 4 languages say: en, nl, fr , de.
by default can use <?php _e('my string here','text_domain'); ?>
, return exact translated text when in domain.
how can translated texts in 1 place. if on english version of website translated content string in nl, fr, de , en.
may know how possible?
you temporary change current language retrieve translated string. like:
// backup current language $current_lang = $sitepress->get_current_language(); // it's "en" // switch language. e.g. $desired_lang = "nl"; $sitepress->switch_lang( $desired_lang ); // translated string... _e( 'my string here', 'text_domain' ); // original language not interfere $sitepress->switch_lang( $current_lang );
i've tested on page template (say index.php
) , works... tried build function job. like:
// put in functions.php function get_all_translations( $string, $languages ) { global $sitepress; if ( empty( $languages ) ) { $languages = array_keys( icl_get_languages( 'skip_missing=0&orderby=code&order=asc' ) ); } $current_lang = $sitepress->get_current_language(); $translations = []; foreach ( $languages $lang ) { $sitepress->switch_lang( $lang, true ); $translations[$lang] = __( $string, 'text_domain' ); } $sitepress->switch_lang( $current_lang ); return $translations; }
and:
// on index.php: var_dump( get_all_translations( 'my string here' ) ); var_dump( get_all_translations( 'my string here', ['nl', 'fr'] ) );
but doesn't work , can't figure out reason... hope helps anyway.
Comments
Post a Comment