Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Load the translations, if there are any.
add_action( 'init', 'dcwd_i18n_demo_load_theme_textdomain' );
function dcwd_i18n_demo_load_theme_textdomain() {
load_plugin_textdomain( 'i18n-demo', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
add_action( 'wp_head', 'dcwd_internationalized_code' );
function dcwd_internationalized_code() {
?>
<?php // For the simple comment we just convert it into a string that is echoed. ?>
<!-- <?php _e( 'This is a simple comment.', 'i18n-demo' ); ?> -->
<?php
// Here I chose to exclude the comment codes in case the translator mistakenly edits them.
printf( "<!-- " . __( 'This site is running WordPress version %s', 'i18n-demo' ) . "-->\n", get_bloginfo( 'version' ), "\n" );
$hour = date( 'G' ); // Get the hour.
if ( $hour < 6 ) {
// For this string I provide some context for the translator.
echo "<!-- " . _x( 'Good night.', 'A greeting for before 6:00.', 'i18n-demo' ) . " -->\n";
}
if ( ( $hour >= 6 ) && ( $hour <= 12 ) ) {
echo "<!-- " . _x( 'Good morning.', 'A greeting for between 6:00 and 11:59.', 'i18n-demo' ) . " -->\n";
}
if ( ( $hour > 12 ) && ( $hour < 18 ) ) {
echo "<!-- " . _x( 'Good afternoon.', 'A greeting for between 12:00 and 17:59.', 'i18n-demo' ) . " -->\n";
}
if ( $hour >= 18 ) {
echo "<!-- " . _x( 'Good evening.', 'A greeting for after 18:00.', 'i18n-demo' ) . " -->\n";
}
// Here I omitted the '%s' and included a context note.
// Maybe I should leave it in the translatable string in case some languages need to change the order.
printf( "<!-- " . _x( 'Today is', 'The day of the week will be appended.', 'i18n-demo' ) . " %s -->\n", date( 'l' ) ); // Print the day of the week.
}
# Based on: https://github.com/naokomc/Blank-WordPress-Pot/blob/master/Blank-WordPress.pot
# i18n Demo Blank Pot
# Copyright (C) 2020 ...
# This file is distrubuted under the GPL-2.0+
msgid ""
msgstr ""
"Project-Id-Version: i18n-demo Pot v1.0.0\n"
"POT-Creation-Date: 2020-07-26 12:00+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Damien Carbery <damien@damiencarbery.com>\n"
"Language-Team: Damien Carbery <damien@damiencarbery.com>\n"
"Report-Msgid-Bugs-To: Damien Carbery <damien@damiencarbery.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Textdomain-Support: yes"
"X-Generator: Poedit 2.4\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;_x;esc_html_e;esc_html__;esc_attr_e;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2;\n"
"X-Poedit-Basepath: ../\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-Language: English\n"
"X-Poedit-Country: UNITED STATES\n"
"X-Poedit-Bookmarks: \n"
<!-- Locale: en_US -->
<!-- This is a simplé cómmént. -->
<!-- This sité is running WórdPréss vérsión 5.4.2-->
<!-- Góód événing. -->
<!-- Tóday is Saturday -->
<!-- Locale: en_US -->
<!-- This is a simple comment. -->
<!-- This site is running WordPress version 5.4.2-->
<!-- Good evening. -->
<!-- Today is Saturday -->
<?php
/*
Plugin Name: i18n demo
Plugin URI: https://www.damiencarbery.com/2020/07/how-to-internationalize-your-code/
Description: A demo on how to internationalize, localize (and test) your WordPress code.
Author: Damien Carbery
Version: 0.1
*/
add_action( 'wp_head', 'dcwd_code_to_internationalize' );
function dcwd_code_to_internationalize() {
?>
<!-- This is a simple comment. -->
<?php
printf( "<!-- This site is running WordPress version %s -->\n", get_bloginfo( 'version' ), "\n" );
$hour = date( 'G' ); // Get the hour.
if ( $hour < 6 ) {
echo "<!-- Good night. -->\n";
}
if ( ( $hour >= 6 ) && ( $hour <= 12 ) ) {
echo "<!-- Good morning. -->\n";
}
if ( ( $hour > 12 ) && ( $hour < 18 ) ) {
echo "<!-- Good afternoon. -->\n";
}
if ( $hour >= 18 ) {
echo "<!-- Good evening. -->\n";
}
printf( "<!-- Today is %s -->\n", date( 'l' ) ); // Print the day of the week.
}
# i18n Demo Blank Pot
# Copyright (C) 2020 ...
# This file is distrubuted under the GPL-2.0+
msgid ""
msgstr ""
"Project-Id-Version: i18n-demo Pot v1.0.0\n"
"POT-Creation-Date: 2020-07-26 12:00+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Damien Carbery <damien@damiencarbery.com>\n"
"Language-Team: Damien Carbery <damien@damiencarbery.com>\n"
"Report-Msgid-Bugs-To: Damien Carbery <damien@damiencarbery.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Textdomain-Support: yes\n"
"X-Generator: Poedit 2.4\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;_x;esc_html_e;esc_html__;esc_attr_e;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2;\n"
"X-Poedit-Basepath: ../\n"
"X-Poedit-SearchPath-0: .\n"
"X-Poedit-Language: English\n"
"X-Poedit-Country: UNITED STATES\n"
"X-Poedit-Bookmarks: \n"
#: i18n-demo.php:51
msgid "This is a simple comment."
msgstr ""
#: i18n-demo.php:54
#, php-format
msgid "This site is running WordPress version %s"
msgstr ""
#: i18n-demo.php:59
msgctxt "A greeting for before 6:00."
msgid "Good night."
msgstr ""
#: i18n-demo.php:62
msgctxt "A greeting for between 6:00 and 11:59."
msgid "Good morning."
msgstr ""
#: i18n-demo.php:65
msgctxt "A greeting for between 12:00 and 17:59."
msgid "Good afternoon."
msgstr ""
#: i18n-demo.php:68
msgctxt "A greeting for after 18:00."
msgid "Good evening."
msgstr ""
#: i18n-demo.php:73
msgctxt "The day of the week will be appended."
msgid "Today is"
msgstr ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.