Last tended on 23 October, 2019 (first created 23 October, 2019)

,

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; ?>

2 reactions on “Language adaptations

,

  1. I made a tiny tool for myself today, to make it easier to display both HTML and PHP code in this site (if you just input it as is it will get filtered, as it would be unsafe otherwise)
    It’s a small webform that lives as a shortcut in my browser bar:

    Which calls itself when submitted, and if there is some input it will encode it by calling the PHP function htmlentities twice (otherwise it would just show the right output, but not what is needed to get that output). The result is shown above the form. Maybe I’ll add a step to put it on the clipboard instead. That would save another step. Currently I run it on an internet accessible webserver, I will likely move it to the webserver I run locally on my laptop Moved it my laptop’s local webserver (making it both safer, and independent from having internet access).
    <?php echo "<pre>".htmlentities(htmlentities($_POST["input_tekst"], ENT_QUOTES))."</pre>"; ?>
    This makes it possible to document code changes much better in my site. Such as my recent language adaptations.
    The code snippet above has of course been made with this form.

  2. A summary overview of changes I made to this site, to make it more fully a indieweb hub / my core online presence. The set-up of my WordPress installation also has been described.
    Theme related tweaks

    Created child theme of Sempress, to be able to change appearance and functions
    Renamed comments to reactions (as they contain likes, reposts, mentions etc.)
    in the entry-footer template and the comments template
    Removed h-card microformats, and put in a generic link to my about page for the author in the Sempress function sempress_posted_on. Without a link to the author mentions show up as anonymous elsewhere.
    Removed the sharing buttons I used (although they were GDPR compliant using the Sharriff plugin, but they got in the way a lot I felt.
    Added a few menu options for various aspects of my postings (books, check-ins, languages)
    Introduced several categories to deal with different content streams: Dutch, German for non-English postings, Day to Day for things not on the home page, Plazes for check-ins, Books for ehh books, RSS-Only for unlisted postings, and Micromessage for tweets I send from the blog. This allows me to vary how I display these different types of things (or not)
    Displaying last edited and created dates to (wiki)pages
    Added a widget with projects I support
    Added to the single post template a section that mentions and links the number of Hypothes.is annotations for that post, where they exist.

    Functionality related tweaks

    Started creating pages as a wiki-like knowledgebase, using page categories to create the wiki structure
    To show excerpts from webmentions I changed the template for a webmention in the Semantinc backlinks plugin, class-linkbacks-handler.php
    Added a plugin to display blogposts on the same date in previous years.
    Added plugin Widget Context to remove recent posts and comments from individual posting’s pages, as they cause trouble with parsing them for webmentions.
    Using categories as differentiator I added language mark-up to individual postings, category archives. Also added automatic translation links to non-English postings in the RSS feed (not on the site). On the front page non-English postings have language mark-up around the posting.
    Added a blogroll that is an OPML file with a stylesheet, so it can be equally read by humans and machines.
    Added an extra RSS feed for comments that excludes webmentions and ping/trackbacks
    Added a /feeds page
    Added a Now page
    Added a Hello page
    Added a way to share book lists / feeds.
    Stopped embedding slide decks, and stopped embedding new Flickr photos (as well as removed older embeds, currently 23 postings between January 2013 and July 2018 still have them, and 22 postings from June 2006 to July 2009)
    Removed all affilliate links to Amazon books as it entails tracking
    Added an Index (using a plugin)
    Added my own basic check-in and Dopplr style posts

    Other tweaks

    Set up 2 additional WordPress instances for testing purposes (Proto and Meso)

Comments are closed.

Mentions