wp-multilang icon indicating copy to clipboard operation
wp-multilang copied to clipboard

Load translation from other language

Open NielsHavermans opened this issue 6 years ago • 4 comments

How can I load a translation from an other language, other than the current language?

For example, my site has 4 languages but some items are only available in 2 languages. If the current language is not available I want to load an other language which is available. I want to use this for the title, content and some meta fields.

NielsHavermans avatar Feb 15 '19 11:02 NielsHavermans

I use this snippet:

//WP Multilang: Fallback to any language if a translation does not exist
add_filter( 'wpm_untranslated_text', function( $translation, $strings, $language ) {
    if ( $translation === '' ) {
        foreach( $strings as $string ) {
            if ( ( '' === $string ) ) continue;

            $translation = $string;
            break;
        }
    }
    return $translation;
}, 10, 3 );

soft79 avatar Mar 02 '19 13:03 soft79

Hi Soft79, thanks for your reply but this is not what i'm looking for. I want to get a other translation (as fallback) from the database only if a translation not exists. So if a page is loaded in Dutch and the page title is not in Dutch available the site needs to load the original English translation as fallback, right from the database.

NielsHavermans avatar Mar 06 '19 08:03 NielsHavermans

And what's wrong with the snippet then? That's what the snippet should do and I have been using it in production for more than a year now.

(originally I placed a PR for this, see https://github.com/VaLeXaR/wp-multilang/pull/12 but VaLeXaR didn't want this in core so he made a filter for this)

soft79 avatar Mar 06 '19 12:03 soft79

Niels, did you try the snippet?

soft79 avatar Apr 02 '19 15:04 soft79