Translation does not show

Hi!

I have problem with my translation. I have followed the instruction https://theme.co/apex/forum/t/setup-translation/61 and I have saved my .po and .mo to the ftp. have checked that they are there although I log-out from FTP and log-in again.

But I can not see them on the page. I have settings that the language of the page is Finnish.

There is this child page info that I try to do next but this “here” link does not work:

“This section presumes you already have a child theme setup and does not cover setting up a child theme if you don’t have a child theme setup, you can download one from the member’s area here”

Were I can have the child theme?

Hey Anni,

We’re sorry the link to the child theme article is broken in the translation article. You can download the child theme at https://theme.co/apex/child-themes so you can continue with the translation setup.

Thanks.

Hi!

Thank you. I have now added the child theme, and the language folder in it. I update the php, but still, I can not see the translated content on my page. It´s still in English. I have emptied the cache.

The functions.php looks like that, maybe that is the problem:

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to X in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================


add_action( 'after_setup_theme', 'load_child_language' );
    function load_child_language() {
        load_child_theme_textdomain( '__x__', get_stylesheet_directory() . '/languages' );
}

That looks correct. In this case, please give us WordPress Admin and FTP access in a Secure Note so we could check why it’s not working.

Thanks.

Hey Anni,

Thanks for the credentials. I checked your site and I don’t see an English text. Everything is translated.

Is there a specific page or a particular area that you can’t translate? Please give us the URLs and screenshots.

Thanks.

Hi!

The problem is on the blog pages, for example:

http://koulu.wpengine.com/2018/08/

Hi Anni,

Looks like conflict between loco translate and your language files and they aren’t synced. Could you try this, but make sure to back-up your language files first.

Go to Admin > Logo Translate > Themes then click X - Child Theme, and then click the first Finnish (Finland) above the Additional files found. Once you’re there, click the Sync button and the Save button once it’s done syncing.

And last, clear your caches before testing.

Thanks!

Hi!

I did that and still no. I can see the correct text in Loco Translate. When I click sync, I get a message: it´s already sync with the source code. It does not let me save.

Still can not see any change after empting the cache.

Hi Anni,

I checked and it’s weird, the strings are translated but won’t change. And regardless of any changes I made it’s not taking effect. I’ll continue checking but for the meantime, please add this code to your child theme’s functions.php as temporary workaround.

add_filter( 'gettext', 'x_translate_these_strings', 20, 3 );

function x_translate_these_strings( $translated_text, $text, $domain ) {

	$locale = get_locale();

    $string_table =  array (
    		'fi' => array (

    				'Post Archive by Month' => 'your translation here',
    				'Read More' => 'your translation here',
    				'Below you\'ll find a list of all posts from ' => 'your translation here',

    		)
    );

    $translation = $string_table[ $locale ][ $translated_text ];

    return !empty( $translation ) ? $translation : $translated_text;

}



But make sure to change the translation :slight_smile:

Thanks!

Hello,

I’m having a very similar issue. I’ve tried with both mo/po files as well as this string translation.

Could it be a priority problem? What is the priority between these locations?

  • /wp-content/languages/
  • /wp-content/languages/plugins/
  • /wp-content/languages/themes/
  • /wp-content/themes/x-child/languages/

Btw, is there any drawback using the string translation, is it slower or something?

I used this code, what’s the difference in functionality?

add_filter( 'gettext', 'x_translate_text', 20, 3 );

function x_translate_text ( $translated_text, $text, $domain ) {

  $translation = array (
	'Brand' => 'Varumärke',
	'Add to cart' => 'Köp'
  );

  if( isset( $translation[$text] ) ) { return $translation[$text]; }

  return $translated_text;

}

I only having issues with one term which happens to be singular/plural. To account for that I found this snippet:

add_filter( 'ngettext', 'x_translate_text_plurals', 20, 3);

function x_translate_text_plurals ( $translated_text, $single, $plural ) { 

  $translation = array(
	'%d Item' => '%d',
	'%d Items' => '%d'
  );

  if( isset( $translation[$single] ) ) { return $translation[$single]; }
  if( isset( $translation[$plural] ) ) { return $translation[$plural]; }
  return $translated_text;
}

The strange thing is that I have a dev site on the same server with almost the same setup, and it get’s correctly translated even though I remove both mo/po files and string translation. Have I forgotten some location for translations? Is it a cache problem?

Hi Gabriel,

Language files are handled automatically by Wordpress depending on default language. With the translation plugin then yes, it could be due to priorities and that’s something we can’t control, the plugin author should be contacted regarding the issue.

As for language folder, what I’m doing is adding the same language files (exact copy with translations) to each folder make sure it works since I myself isn’t sure what Wordpress pickup last (priority), it’s Wordpress standard. So I recommend translate single files and copy it to each folder, but make sure to change filenames depending on textdomain, theme, or plugin.

As for the above code, it’s Wordpress given filter/hook. And the translation plugins are using the same except it uses language files and database for string look up for matching the translation. While that code is simply picking the static string you manually replace with the translation.

You can also change the priority of the code/filter, example,

add_filter( 'ngettext', 'x_translate_text_plurals', 999999, 3);

Which means your custom function/filter will be executed last that replaces any string before it. And yes, it could be due to cache too or transients, there are many strings to translate and will be slow if executed over and over every site’s load, so it’s usually cached to prevent it from retranslating when it’s already translated. But that’s just my guest about translation plugins.

Thanks!

Hi,

Thanks for a quick response.

I noticed that the correct “translation” from “%d Item” to " Item" flickered after having emptied the cache and reloading the page, and then the wrong translation was added again. I forced the browser to fetch new files, but it didn’t help. I then logged out and emptied the browser cache and that seemed to do the trick.

Changing priority of the filter didn’t affect anything. The funny thing is that I now deleted child translations, removed the specific filter, emptied browser cache, logged out used different browser and anonymous window. And still the translation is correct!? Weirdness …

I’m not using a translation plugin, btw. Also, since I only have 30 or so manual translations it’s way easier to use the filter in a snippet. All hassle with updating translation files an recompiling mo files are gone.

In any case, now it seems to work.

Thanks.

Oh, I guess your version of the filter only translates given the specific locale, i.e. FI, while in my case the translation will take place no matter the locale. Correct?

Thanks,
G.

Hi Gabriel,

The flickering shouldn’t happen since it’s a server cache and the translation is done through server. Perhaps you have some translation done through javascript or jQuery?

And yes, the code will only work for locale FI and it’s done that way since it’s not using any WPML. Plus, locale can be overriden so it’s only given and tested to his current setup and may vary.

Thanks!

I don’t have any JS or jQuery translation that I’m aware of, strange …

Would you say there is anything wrong with the code for singular/plural translations above?

G.

Hi Gabriel,

It’s may not work because the translation is already called before the hook is invoked. So checking the existence of %d is not valid anymore. The code responsible for that is this

362	function _n( $single, $plural, $number, $domain = 'default' ) {
363	        $translations = get_translations_for_domain( $domain );
364	        $translation  = $translations->translate_plural( $single, $plural, $number );
365	
366	        /**
367	         * Filters the singular or plural form of a string.
368	         *
369	         * @since 2.2.0
370	         *
371	         * @param string $translation Translated text.
372	         * @param string $single      The text to be used if the number is singular.
373	         * @param string $plural      The text to be used if the number is plural.
374	         * @param string $number      The number to compare against to use either the singular or plural form.
375	         * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
376	         */
377	        return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
378	}

Hence, please change your code to this

add_filter( 'ngettext', 'x_translate_text_plurals', 999, 3);

function x_translate_text_plurals ( $translated_text, $single, $plural ) { 

if ( preg_match("/^\d Item[s]?$/", $translated_text ) ) {

$translated_text = preg_replace( '/ Item[s]?$/', '', $translated_text );

}

return $translated_text;

}

Thanks!

1 Like

Thanks for the update. It’s weird, it seems to be working after all. If not I’ll give your alternative a try.

Wonderful response anyhow!

G.

You are most welcome. :slight_smile:

Hi!

Sorry my answer has taken so long. I managed to do the translation with your code. Thank you so much.

I have the same issues here, could you please send me the same code for that:
https://koulu.wpengine.com/category/kotitiedote/

Hey Anni,

Basically, you just need to copy the text in the front-end and add it in the code provided by Rad but making sure you follow the syntax like this:

If it does not work, you will need to check what’s in the .pot file because sometimes, it might include a space.

As you can see there, I used the HTML entity of the non-breaking space &nbsp; because inputting a space after the translated text doesn’t work so it’s also something to take into account.

Hope that helps.