I post in three languages on this site: English (mostly), Dutch, German (sometimes).
To better allow language detection I have added mark-up for it in various places. I also add translation links to the RSS feed items that are not in English.
Note: All my non-English posts are in specific categories that provide the language (e.g. nederlands for Dutch, and deutsch for German)
Adaptations made:
Translation links in RSS feed items
In functions.php of my (child)-theme I added (based on this code by Jan Boddez):
add_filter( 'the_content_feed', 'my_content_feed' );
function my_content_feed( $content ) {
global $post;
$testurl = get_permalink($post);
if ( has_category( 'nederlands', $post->ID)) {
$basetranslate = 'https://translate.google.com/translate?sl=nl&tl=en&u=';
$testedurl = urlencode($testurl);
$basetranslate = $basetranslate.$testedurl;
$addedlink= '<br/><a href="'.$basetranslate.'">machine translation into English</a><br/>';
$content = $content.$addedlink;
}
if ( has_category( 'deutsch', $post->ID)) {
$basetranslate = 'https://translate.google.com/translate?sl=de&tl=en&u=';
$testedurl = urlencode($testurl);
$basetranslate = $basetranslate.$testedurl;
$addedlink= '<br/><a href="'.$basetranslate.'">machine translation into English</a><br/>';
$content = $content.$addedlink;
}
return $content;
}
Setting page wide default language
The function for setting a page wide default considers two cases: when displaying a single post, and when displaying a category archive.
In functions.php of my (child)-theme I added:
function tonsempress_language_attributes($langtoets){
// ton: if called from single or cat will have right language otherwise default
$langtoets ="en-us";
if (is_single()){
if ( in_category('nederlands') ) {
$langtoets = "nl-nl";}
if ( in_category('deutsch') ) {
$langtoets = "de-de";}
}
if (is_category('nederlands')){$langtoets = "nl-nl";}
if (is_category('deutsch')){$langtoets = "de-de";}
// return the new language attribute
return 'lang="'.$langtoets.'"';
}
add_filter('language_attributes', 'tonsempress_language_attributes');
Setting item language on front page
Items on the front page (!) that are in Dutch or German will get a span with their language attribute set accordingly. I changed this in index.php of the child theme, where it considers if a Dutch or German category post is being displayed, in the WordPress Loop:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to overload this in a child theme then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
?>
<?php /* if in Dutch or German categories, on the homepage added Oct 2019*/?>
<?php if ( is_home() ) {
if ( in_category( 'nederlands' ) ) { ?><span lang="nl-nl"><?php }
if ( in_category( 'deutsch' ) ) { ?><span lang="de-de"><?php }
}
get_template_part( 'content', get_post_format() );
/* if in Dutch or German categories, on the homepage added Oct 2019*/
if ( is_home() ) {
if ( in_category( 'nederlands' ) ) { ?></span><?php }
if ( in_category( 'deutsch' ) ) { ?></span><?php }
}
?>
<?php endwhile; ?>
Mentions