[gnomeweb-wp: 7/13] Initial commit of the translation plugin
- From: Vinicius Scopel Depizzol <vdepizzol src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnomeweb-wp: 7/13] Initial commit of the translation plugin
- Date: Wed, 26 Jan 2011 23:01:05 +0000 (UTC)
commit 36f6ad971fac6eadb79ea46ee028785757a844f6
Author: Lincoln de Sousa <lincoln comum org>
Date: Fri Nov 12 19:26:39 2010 -0200
Initial commit of the translation plugin
This comes with a file that builds an xml with contents of all posts
and with a plugin to wordpress that uses xml2po to convert this xml
to a po file. This also contains hooks to the_{title,content} that
uses gettext() to get translated versions of the site content.
wp-content/plugins/wppo/wppo.genxml.php | 82 ++++++++++++++++++++++++++++
wp-content/plugins/wppo/wppo.php | 88 +++++++++++++++++++++++++++++++
2 files changed, 170 insertions(+), 0 deletions(-)
---
diff --git a/wp-content/plugins/wppo/wppo.genxml.php b/wp-content/plugins/wppo/wppo.genxml.php
new file mode 100644
index 0000000..946f8f7
--- /dev/null
+++ b/wp-content/plugins/wppo/wppo.genxml.php
@@ -0,0 +1,82 @@
+<?php
+/* Copyright 2010 Vinicius Depizzol <vdepizzol gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// This code by now only generates an XML with all the content from
+// WordPress database.
+
+require_once(ABSPATH . "wp-config.php");
+
+function wppo_generate_po_xml () {
+ global $wpdb;
+
+ $myrows = $wpdb->get_results("
+ SELECT ID, post_content, post_title, post_excerpt, post_name
+ FROM wp_posts
+ WHERE post_type != 'revision' && post_type != 'nav_menu_item'
+ ");
+
+ header("Content-type: text/xml");
+
+ $broken_dom_pages = array();
+
+ $dom = new DOMDocument('1.0', 'utf-8');
+ $dom->formatOutput = true;
+ $root = $dom->createElement("website");
+
+ foreach($myrows as $key => $value) {
+ $page = $dom->createElement("page");
+
+ // ID
+ $page_id = $dom->createAttribute('id');
+ $page_id_value = $dom->createTextNode($value->ID);
+ $page_id->appendChild($page_id_value);
+ $page->appendChild($page_id);
+
+ // page_title
+ $page_title = $dom->createElement("title");
+ $page_title_value = $dom->createTextNode($value->post_title);
+ $page_title->appendChild($page_title_value);
+ $page->appendChild($page_title);
+
+ // page_name
+ $page_name = $dom->createElement("name");
+ $page_name_value = $dom->createTextNode($value->post_name);
+ $page_name->appendChild($page_name_value);
+ $page->appendChild($page_name);
+
+ // page_content
+ $value->post_content = wpautop($value->post_content);
+
+ $content_xml = $dom->createDocumentFragment();
+ $content_xml->appendXML('<html>'.$value->post_content.'</html>');
+
+ if ($content_xml == false) {
+ $broken_dom_pages[] = $value->ID;
+ }
+
+ $page_content = $dom->createElement("content");
+ $page_content->appendChild($content_xml);
+ $page->appendChild($page_content);
+
+ $root->appendChild($page);
+ }
+
+ $dom->appendChild($root);
+ $content = $dom->saveXML();
+ return $content;
+}
+?>
diff --git a/wp-content/plugins/wppo/wppo.php b/wp-content/plugins/wppo/wppo.php
new file mode 100644
index 0000000..3321e6e
--- /dev/null
+++ b/wp-content/plugins/wppo/wppo.php
@@ -0,0 +1,88 @@
+<?php
+/*
+Plugin Name: WPPO
+Description: A hack to make wordpress become a multilingual site using gettext
+Version: 0.1
+Author: Lincoln de Sousa <lincoln comum org>
+Author URI: http://lincoln.comum.org
+License: AGPLv3
+*/
+
+/* Copyright 2010 Lincoln de Sousa <lincoln comum org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+require_once ("wppo.genxml.php");
+
+define (PO_DIR, ABSPATH . "po/");
+
+/* Setting up where compiled po files are located and which translation
+ * domain to use. */
+bindtextdomain ('gnomesite', PO_DIR);
+bind_textdomain_codeset ('gnomesite', 'UTF-8');
+textdomain ('gnomesite');
+
+/* 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 = PO_DIR . ".tmp.xml";
+ $pot_file = PO_DIR . "gnomesite.pot";
+ file_put_contents ($xml_file, wppo_generate_po_xml ());
+ exec ("/usr/bin/xml2po -o $pot_file $xml_file");
+ unlink ($xml_file);
+}
+add_action ('post_updated', 'wppo_update_pot_file');
+
+/* Using gettext to get the translated version of received strings */
+function wppo_get_translated_string ($content) {
+ $lang = isset ($_REQUEST['lang']) ? $_REQUEST['lang'] : $_COOKIE['lang'];
+ if (!$lang)
+ return $content;
+
+ setlocale (LC_MESSAGES, $lang);
+
+ /* If there's a new line in the content, we use wpautop() function,
+ * because the script that generates the xml with translatable strings
+ * has to call it otherwise we'll lose paragraphs inserted by the
+ * user. */
+ if (stristr ($content, "\n") === FALSE)
+ return gettext ($content);
+ else
+ $content = wpautop ($content);
+
+ /* Parsing the content to split up <p> tags */
+ $newct = '';
+ $parser = xml_parser_create ();
+ xml_parse_into_struct ($parser, "<r>$content</r>", $vals, $index);
+ foreach ((array) $index['P'] as $p)
+ $newct .= "<p>" . gettext ($vals[$p]['value']) . "</p>\n";
+ xml_parser_free ($parser);
+ return $newct;
+}
+add_filter ('the_title', 'wppo_get_translated_string', 1);
+add_filter ('the_content', 'wppo_get_translated_string', 1);
+
+/* Saving the language code choosen by the user */
+if (isset ($_REQUEST['lang']))
+ setcookie ("lang", $_REQUEST['lang'], time () + 36000, "/");
+
+/* A nice url to show the pot file */
+if (isset ($_REQUEST['pot'])) {
+ header ("Content-Type: text/plain");
+ die (file_get_contents (PO_DIR . "gnomesite.pot"));
+}
+
+?>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]