[gnomeweb-wp] Apply wordpress filters cms-wide to use translated strings without changes in template functions
- From: Vinicius Scopel Depizzol <vdepizzol src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnomeweb-wp] Apply wordpress filters cms-wide to use translated strings without changes in template functions
- Date: Sun, 13 Mar 2011 18:58:07 +0000 (UTC)
commit 004a3b5e57c7a5bf593d0cc1d7c2837b3d0f3850
Author: Vinicius Depizzol <vdepizzol gmail com>
Date: Sun Mar 13 15:56:26 2011 -0300
Apply wordpress filters cms-wide to use translated strings without changes in template functions
This is a major change made by Andre Metzen <andressantos gmail com> and
Henrique Netto <dantesmh gmail com>. Instead of using custom functions to
return translated content, WPPO now uses wordpress filters[1] to apply
translations in all instances of the website.
By now, there is no need to change template functions in order to provide
translated pages. Any template can be translatable without changes in its code.
This also solves some problems like the rendering of menus and excerpts using
translated strings.
* There are some known bugs, which will be listed in the TODO file.
[1] http://codex.wordpress.org/Plugin_API/Filter_Reference
wp-content/plugins/wppo/wppo.php | 183 +++++++++++++-------
wp-content/themes/gnome-grass/functions.php | 19 --
wp-content/themes/gnome-grass/home.php | 4 +-
wp-content/themes/gnome-grass/index.php | 4 +-
wp-content/themes/gnome-grass/news.php | 4 +-
.../themes/gnome-grass/one-column-with-submenu.php | 4 +-
wp-content/themes/gnome-grass/one-column.php | 4 +-
wp-content/themes/gnome-grass/project.php | 4 +-
wp-content/themes/gnome-grass/search.php | 4 +-
wp-content/themes/gnome-grass/style.css | 5 +-
10 files changed, 135 insertions(+), 100 deletions(-)
---
diff --git a/wp-content/plugins/wppo/wppo.php b/wp-content/plugins/wppo/wppo.php
index 489211c..798abef 100644
--- a/wp-content/plugins/wppo/wppo.php
+++ b/wp-content/plugins/wppo/wppo.php
@@ -24,13 +24,26 @@ License: AGPLv3
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+if(!function_exists ('_log')) {
+ function _log ( $message ) {
+ if(WP_DEBUG === true) {
+ if( is_array($message) || is_object ($message) ){
+ file_put_contents (ABSPATH.'debug.log', print_r ($message, true)."\n", FILE_APPEND);
+ } else {
+ file_put_contents (ABSPATH.'debug.log', $message."\n" , FILE_APPEND);
+ }
+ }
+ }
+}
+
+
require_once ("wppo.genxml.php");
-define (WPPO_DIR, ABSPATH . "wppo/");
-define (PO_DIR, WPPO_DIR . "po/");
-define (POT_DIR, WPPO_DIR . "pot/");
-define (POT_FILE, POT_DIR . "gnomesite.pot");
-define (XML_DIR, WPPO_DIR . "xml/");
+define ('WPPO_DIR', ABSPATH . "wppo/");
+define ('PO_DIR', WPPO_DIR . "po/");
+define ('POT_DIR', WPPO_DIR . "pot/");
+define ('POT_FILE', POT_DIR . "gnomesite.pot");
+define ('XML_DIR', WPPO_DIR . "xml/");
$wppo_cache = array();
@@ -39,6 +52,7 @@ $wppo_cache = array();
bindtextdomain ('gnomesite', PO_DIR);
bind_textdomain_codeset ('gnomesite', 'UTF-8');
textdomain ('gnomesite');
+add_filter ('get_pages', 'wppo_filter_get_pages', 1);
/* Creates wppo auxiliary table when plugin is installed to keep all the
@@ -68,21 +82,83 @@ function wppo_install () {
}
if (!is_dir (WPPO_DIR)) {
- @mkdir (WPPO_DIR, 0777);
- @mkdir (PO_DIR, 0777);
- @mkdir (POT_DIR, 0777);
- @mkdir (XML_DIR, 0777);
- }
-
+ mkdir (WPPO_DIR, 0777);
+ mkdir (PO_DIR, 0777);
+ mkdir (POT_DIR, 0777);
+ mkdir (XML_DIR, 0777);
+ }
wppo_update_pot_file ();
}
register_activation_hook (__FILE__, 'wppo_install');
+add_filter ('the_title', function($title, $id) {
+ global $wppo_cache;
+
+ $translated_title = trim (wppo_get_translated_data ('translated_title', $id));
+
+ if(empty ($translated_title)) {
+ return $title;
+ } else {
+ return $translated_title;
+ }
+}, 10, 2);
+
+add_filter ('the_content', function($content) {
+ global $wppo_cache, $post;
+
+ if(isset($wppo_cache[$post->ID])) {
+ $translated_content = $wppo_cache[$post->ID]['translated_content'];
+ } else {
+ $translated_content = trim (wppo_get_translated_data ('translated_content', $post->ID));
+ }
+
+ if(empty ($translated_content)) {
+ return $content;
+ } else {
+ return $translated_content;
+ }
+}, 10, 1);
+
+
+
+function wppo_filter_get_pages ($pages) {
+ global $wppo_cache, $wpdb;
+
+ $lang = wppo_get_lang ();
+ if(strpos ($lang, '_') !== false) {
+ $fallback_lang = explode ('_', $lang);
+ $fallback_lang = $fallback_lang[0];
+ } else {
+ $fallback_lang = $lang;
+ }
+
+ foreach($pages as $page)
+ {
+ if(!isset ($wppo_cache[$page->ID])) {
+ $wppo_cache[$page->ID] = $wpdb->get_row ("SELECT * FROM " . $wpdb->prefix . "wppo WHERE post_id = '" . $page->ID . "' AND (lang = '" . $lang . "' OR lang = '" . $fallback_lang . "')", ARRAY_A);
+ }
+
+ if(is_array($wppo_cache[$page->ID])) {
+ $page->post_title = $wppo_cache[$page->ID]['translated_title'];
+ $page->post_name = $wppo_cache[$page->ID]['translated_name'];
+ $page->post_content = $wppo_cache[$page->ID]['translated_content'];
+ $page->post_excerpt = $wppo_cache[$page->ID]['translated_excerpt'];
+ }
+
+
+
+ }
+
+ //_log($pages);
+ return $pages;
+}
+
/* This action will be fired when a post/page is updated. It's used to
* update (regenerate, actually) the pot file with all translatable
* strings of the gnome.org website. */
function wppo_update_pot_file ($post) {
+
$xml_file = XML_DIR . "gnomesite.xml";
file_put_contents ($xml_file, wppo_generate_po_xml ());
exec ("/usr/bin/xml2po -m xhtml -o " . POT_FILE . " $xml_file");
@@ -101,10 +177,11 @@ function wppo_update_pot_file ($post) {
}
/* This shouldn't be here. FIXME */
- wppo_receive_po_file ();
-
+ //wppo_receive_po_file ();
+
}
add_action ('post_updated', 'wppo_update_pot_file');
+add_action ('post_updated', 'wppo_receive_po_file');
/* this action will be fired when damned lies system send an updated version of
@@ -112,13 +189,13 @@ add_action ('post_updated', 'wppo_update_pot_file');
* xml file and separate its content to the wordpress database */
function wppo_receive_po_file () {
global $wpdb;
-
- $table_format = array ('%s', '%d', '%s', '%s', '%s');
+
+ $table_format = array ('%s', '%d', '%s', '%s', '%s');
if ($handle = opendir (PO_DIR)) {
while (false !== ($po_file = readdir ($handle))) {
-
- /* Gets all the .po files from PO_DIR. Then it will generate a translated
+
+ /* Gets all the .po files from PO_DIR. Then it will generate a translated
* XML for each language.
*
* All the po files must use the following format: "gnomesite.[lang-code].po"
@@ -132,11 +209,10 @@ function wppo_receive_po_file () {
*/
$lang = $po_file_array[1];
$translated_xml_file = XML_DIR . 'gnomesite.' . $lang . '.xml';
-
- exec ("/usr/bin/xml2po -m xhtml -p " . PO_DIR . "$po_file -o $translated_xml_file " . XML_DIR . "gnomesite.xml");
-
+ $cmd = "/usr/bin/xml2po -m xhtml -p " . PO_DIR . "$po_file -o $translated_xml_file " . XML_DIR . "gnomesite.xml";
+ $out = exec ($cmd);
+
$translated_xml = file_get_contents ($translated_xml_file);
-
$dom = new DOMDocument ();
$dom->loadXML($translated_xml);
@@ -162,7 +238,7 @@ function wppo_receive_po_file () {
'translated_excerpt' => $page_excerpt,
'translated_name' => $page_name,
'translated_content' => $page_content);
-
+
/* Stores in the table the translated version of the page */
$wpdb->get_row ("SELECT wppo_id FROM " . $wpdb->prefix . "wppo WHERE post_id = '" . $page_id . "' AND lang = '" . $lang . "'");
@@ -208,10 +284,19 @@ function wppo_get_lang () {
/* Get all the translated data from the current post */
-function wppo_get_translated_data ($string) {
+function wppo_get_translated_data ($string, $id = null) {
global $post, $wpdb, $wppo_cache;
$lang = wppo_get_lang ();
+
+ if($id !== null)
+ {
+ $p = &get_post($id);
+ }
+ else
+ {
+ $p = $post;
+ }
if(strpos ($lang, '_') !== false) {
$fallback_lang = explode ('_', $lang);
@@ -219,57 +304,25 @@ function wppo_get_translated_data ($string) {
} else {
$fallback_lang = $lang;
}
-
-
- if(!isset ($wppo_cache[$post->ID])) {
- $wppo_cache[$post->ID] = $wpdb->get_row ("SELECT * FROM " . $wpdb->prefix . "wppo WHERE post_id = '" . $post->ID . "' AND (lang = '" . $lang . "' OR lang = '" . $fallback_lang . "')", ARRAY_A);
- }
-
- if(isset ($wppo_cache[$post->ID][$string]))
- return $wppo_cache[$post->ID][$string];
- else
- return false;
-}
-
-function wppo_get_the_title () {
- global $post, $wpdb;
- $title = wppo_get_translated_data ('translated_title');
- if ($title != false) {
- return $title;
- } else {
- return $post->post_title;
+ if(!isset ($wppo_cache[$p->ID])) {
+ $wppo_cache[$p->ID] = $wpdb->get_row ("SELECT * FROM " . $wpdb->prefix . "wppo WHERE post_id = '" . $p->ID . "' AND (lang = '" . $lang . "' OR lang = '" . $fallback_lang . "')", ARRAY_A);
}
-}
-
-function wppo_get_the_excerpt () {
- global $post, $wpdb;
-
- $content = wppo_get_translated_data ('translated_excerpt');
-
- if ($content != false) {
- return $content;
+ if(isset ($wppo_cache[$p->ID][$string]) && $fallback_lang != "en") {
+ return $wppo_cache[$p->ID][$string];
} else {
- return $post->post_excerpt;
+ if($string == 'translated_content') {
+ return wpautop ($p->post_content);
+ } else {
+ return $p->{str_replace("translated_", "post_", $string)};
+ }
}
-
}
-function wppo_get_the_content () {
- global $post, $wpdb;
-
- $content = wppo_get_translated_data ('translated_content');
-
- if ($content != false) {
- return $content;
- } else {
- return wpautop ($post->post_content);
- }
-
-}
+
/* Using gettext to get the translated version of received strings */
diff --git a/wp-content/themes/gnome-grass/functions.php b/wp-content/themes/gnome-grass/functions.php
index 7226000..2978697 100644
--- a/wp-content/themes/gnome-grass/functions.php
+++ b/wp-content/themes/gnome-grass/functions.php
@@ -72,25 +72,6 @@ function create_post_type() {
}
-if (!function_exists ('wppo_get_the_title')) {
- function wppo_get_the_title () {
- return get_the_title ();
- }
-}
-
-if (!function_exists ('wppo_get_the_excerpt')) {
- function wppo_get_the_excerpt () {
- return get_the_excerpt ();
- }
-}
-
-if (!function_exists ('wppo_get_the_content')) {
- function wppo_get_the_content () {
- return get_the_content ();
- }
-}
-
-
// Beautify [galery] shortcode
add_filter('gallery_style', create_function('$a', 'return preg_replace("%
diff --git a/wp-content/themes/gnome-grass/home.php b/wp-content/themes/gnome-grass/home.php
index 6c0bc02..3748ac6 100644
--- a/wp-content/themes/gnome-grass/home.php
+++ b/wp-content/themes/gnome-grass/home.php
@@ -42,8 +42,8 @@ require_once("header.php"); ?>
<div class="grid_6 omega">
<a href="<?php the_permalink(); ?>">
- <strong><?php echo wppo_get_the_title(); ?></strong><br />
- <?php echo strip_tags(wppo_get_the_excerpt()); ?>
+ <strong><?php the_title(); ?></strong><br />
+ <?php echo strip_tags(get_the_excerpt()); ?>
</a>
</div>
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/index.php b/wp-content/themes/gnome-grass/index.php
index 1168d75..5c04694 100644
--- a/wp-content/themes/gnome-grass/index.php
+++ b/wp-content/themes/gnome-grass/index.php
@@ -11,12 +11,12 @@ require_once("header.php"); ?>
<div class="container_12">
<div class="page_title">
- <h1><?php echo wppo_get_the_title(); ?></h1>
+ <h1><?php the_title(); ?></h1>
</div>
<div class="content">
<?php while ( have_posts() ) : the_post(); ?>
- <?php echo wppo_get_the_content(); ?>
+ <?php the_content(); ?>
<?php endwhile; // End the loop. Whew. ?>
<br />
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/news.php b/wp-content/themes/gnome-grass/news.php
index ff74c77..85a483b 100644
--- a/wp-content/themes/gnome-grass/news.php
+++ b/wp-content/themes/gnome-grass/news.php
@@ -18,9 +18,9 @@ require_once("header.php"); ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="news_title">
<p class="date"><?php the_date(); ?></p>
- <h1><?php echo wppo_get_the_title(); ?></h1>
+ <h1><?php the_title(); ?></h1>
</div>
- <?php echo wppo_get_the_content(); ?>
+ <?php the_content(); ?>
<?php endwhile; // End the loop. Whew. ?>
<br />
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/one-column-with-submenu.php b/wp-content/themes/gnome-grass/one-column-with-submenu.php
index 6b35492..71af7ad 100644
--- a/wp-content/themes/gnome-grass/one-column-with-submenu.php
+++ b/wp-content/themes/gnome-grass/one-column-with-submenu.php
@@ -11,7 +11,7 @@ require_once("header.php"); ?>
<div class="container_12">
<div class="page_title with_subpages_list">
- <h1><?php echo wppo_get_the_title(); ?></h1>
+ <h1><?php the_title(); ?></h1>
<ul class="subpages_list">
<?php
@@ -39,7 +39,7 @@ require_once("header.php"); ?>
<div class="content without_sidebar">
<?php while ( have_posts() ) : the_post(); ?>
- <?php echo wppo_get_the_content(); ?>
+ <?php the_content(); ?>
<?php endwhile; // End the loop. Whew. ?>
<br />
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/one-column.php b/wp-content/themes/gnome-grass/one-column.php
index a16905e..972aee8 100644
--- a/wp-content/themes/gnome-grass/one-column.php
+++ b/wp-content/themes/gnome-grass/one-column.php
@@ -10,12 +10,12 @@ require_once("header.php"); ?>
<div class="container_12">
<div class="page_title">
- <h1><?php echo wppo_get_the_title(); ?></h1>
+ <h1><?php the_title(); ?></h1>
</div>
<div class="content without_sidebar">
<?php while ( have_posts() ) : the_post(); ?>
- <?php echo wppo_get_the_content(); ?>
+ <?php the_content(); ?>
<?php endwhile; // End the loop. Whew. ?>
<br />
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/project.php b/wp-content/themes/gnome-grass/project.php
index 169c3d2..2104084 100644
--- a/wp-content/themes/gnome-grass/project.php
+++ b/wp-content/themes/gnome-grass/project.php
@@ -21,12 +21,12 @@ require_once("header.php"); ?>
echo 'noicon';
}
?>
- <?php echo wppo_get_the_title(); ?></h1>
+ <?php the_title(); ?></h1>
</div>
<div class="content">
<?php while ( have_posts() ) : the_post(); ?>
- <?php echo wppo_get_the_content(); ?>
+ <?php the_content(); ?>
<?php endwhile; // End the loop. Whew. ?>
<br />
<div class="clear"></div>
diff --git a/wp-content/themes/gnome-grass/search.php b/wp-content/themes/gnome-grass/search.php
index 755cfb1..8f22f81 100644
--- a/wp-content/themes/gnome-grass/search.php
+++ b/wp-content/themes/gnome-grass/search.php
@@ -18,8 +18,8 @@ require_once("header.php"); ?>
<div class="content">
<dl>
<?php while ( have_posts() ) : the_post(); ?>
- <dt><a href="<?php the_permalink(); ?>"><?php echo wppo_get_the_title(); ?></a></dt>
- <dd><?php echo wppo_get_the_excerpt(); ?></dd>
+ <dt><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dt>
+ <dd><?php the_excerpt(); ?></dd>
<?php endwhile; // End the loop. Whew. ?>
</dl>
diff --git a/wp-content/themes/gnome-grass/style.css b/wp-content/themes/gnome-grass/style.css
index 253e64b..1f38ced 100644
--- a/wp-content/themes/gnome-grass/style.css
+++ b/wp-content/themes/gnome-grass/style.css
@@ -292,10 +292,11 @@ a:visited {
margin-top: 0;
color: #333;
font-weight: normal;
- font-size: 50px;
+ font-size: 42px;
+ line-height: 100%;
+ letter-spacing: -0.07em;
}
.news_title .date {
- font-family: 'Droid Serif', 'DejaVu Serif', Cambria, Georgia, serif;
margin: 0;
color: #3465a4;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]