root/basic-bilingual/branches/wp-2.0/basic-bilingual.php

Revision 4384, 5.9 kB (checked in by steph, 3 years ago)

Fixed version number. 0.21 works with WP2.0

Line 
1 <?php
2 /*
3 Plugin Name: Basic Bilingual
4 Plugin URI: http://dev.wp-plugins.org/wiki/BasicBilingual
5 Description: Makes managing your blog with two languages less cumbersome.
6 Version: 0.21
7 Author: Stephanie Booth
8 Author URI: http://climbtothestars.org/
9
10
11   Copyright 2005  Stephanie Booth  (email : steph@climbtothestars.org)
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 INFORMATION:
28 ============
29
30 View http://dev.wp-plugins.org/wiki/BasicBilingual for information about this plugin, what it does and how to use it. In short, it has to do with blogging in more than one language.
31
32 CHANGELOG:
33 ==========
34
35 0.1  - Initial release
36 0.2  - Fixed update bug for other-excerpt (function name was wrong in action statement!) 28.01.2005
37 0.21 - Fixed for WP 2.0 by replacing $postdata->ID with $post->ID (31.12.2005)
38      - Cosmetic changes to the edit form (03.01.2006)
39      - added hooks to deal with pages (03.01.2006)
40
41 SETTINGS:
42 =========
43
44 Replace "en" and "fr" with your two languages in the array, with two-letter codes.
45
46 */
47
48 $bb_languages=array('en', 'fr');
49
50
51 // retrieve the language of the post
52 function bb_get_the_language()
53 {   
54     $post_language=get_post_custom_values("language");
55     $language=$post_language['0'];
56     return($language);
57 }
58
59 // return the other language (the one the post isn't in)
60 function bb_get_the_other_language()
61 {
62     global $bb_languages;
63     if(bb_get_the_language()==$bb_languages[0])
64     {
65         $other_language=$bb_languages[1];
66     }else{
67         $other_language=$bb_languages[0];
68     }
69     return($other_language);
70 }
71
72
73 // TEMPLATE FUNCTIONS
74
75 // could probably be cleaner code-wise, but works like that
76 // wrapper for the_time() which takes language, to display date and time in the language of the current post
77 function bb_the_time($format="%A %d.%m.%Y<br />%Hh%M")
78 {
79     global $post;
80     
81     $language=bb_get_the_language();
82     
83     // setlocale needs the language in this format
84     $code = $language . '_' . strtoupper($language);
85     
86     // change countries ;-)
87     setlocale(LC_TIME, $code);
88     
89     // write it out -- this was lifted from the_time() iirc
90     $wp_time=$post->post_date;
91     $timestamp=strtotime($wp_time);
92     $result=strftime($format, $timestamp);
93     print($result);
94 }
95
96 // this one outputs the language
97 function bb_the_language()
98 {
99     $language=bb_get_the_language();
100     print($language);
101 }
102
103 // this outputs the other language excerpt
104 function bb_the_other_excerpt($before='<div class="other-excerpt" lang="%lg"><p>', $after='</p></div>')
105 {
106     $post_other_excerpt=get_post_custom_values("other-excerpt");
107     $the_other_excerpt=$post_other_excerpt['0'];
108     
109     // make sure there is an excerpt to display
110     if(!empty($the_other_excerpt))
111     {
112         // this is the excerpt language (easy, because it's bilingual)
113         $excerpt_language=bb_get_the_other_language();
114         
115         // add a nice little lang attribute where asked for
116         $before=str_replace('%lg', $excerpt_language, $before);
117         $after=str_replace('%lg', $excerpt_language, $after); // doubt this is needed!
118         // stick everything together
119         $the_other_excerpt = $before . $the_other_excerpt . $after;
120         print($the_other_excerpt);
121     }
122 }
123
124 // ADMIN TWEAKING
125
126 // output textarea to easily add other-excerpt in admin menu (addition to the post form)
127 function add_other_excerpt_textarea() {
128     
129     global $post;
130     
131     $excerpt = get_post_meta($post->ID, 'other-excerpt', true);
132     
133     echo '<fieldset id="postotherexcerpt" style="clear: both;"><legend>' . __('Other Language Excerpt', 'BasicBilingual') . '</legend>';
134     echo '<div><textarea rows="4" cols="80" name="other-excerpt" id="other-excerpt">';
135     print($excerpt);
136     echo '</textarea></div></fieldset>';
137
138 }
139
140 // this one outputs a little box for typing in the post language (admin pages)
141 function add_language_box()
142 {
143      global $bb_languages;
144      global $post;
145     
146      // retrieving existing language, or setting to default if new post
147      $current_language=get_post_meta($post->ID, 'language', true);
148      if(empty($current_language))
149      {
150          $current_language=$bb_languages[0];
151      }
152
153      print('<fieldset id="languagediv" style="height: 3.5em; width: 5em; position: absolute; top: 10em; left: 42em;">
154       <legend>');
155       echo __('Language');
156       print('</legend>
157       <div><input type="text" name="language" size="7" value="');
158       print($current_language);
159       print('" id="language" /></div>
160 </fieldset>');
161 }
162
163 // ACTION FUNCTIONS
164
165 // general custom field update function
166 function bb_update_meta($id, $field)
167 {
168     $setting = $_POST[$field];
169     $meta_exists=update_post_meta($id, $field, $setting);
170     if(!$meta_exists)
171     {
172         add_post_meta($id, $field, $setting);   
173     }
174 }
175
176 // update language custom field
177 function bb_update_language($id)
178 {
179     bb_update_meta($id, "language");
180 }
181
182 // update other language excerpt custom field
183 function bb_update_other_excerpt($id)
184 {
185     bb_update_meta($id, "other-excerpt");
186 }
187
188 add_action('simple_edit_form', 'add_other_excerpt_textarea');
189 add_action('edit_form_advanced', 'add_other_excerpt_textarea');
190 add_action('simple_edit_form', 'add_language_box');
191 add_action('edit_form_advanced', 'add_language_box');
192 add_action('edit_page_form', 'add_language_box');
193 add_action('edit_page_form', 'add_other_excerpt_textarea');
194
195 add_action('edit_post', 'bb_update_language');
196 add_action('save_post', 'bb_update_language');
197 add_action('publish_post', 'bb_update_language');
198
199 add_action('edit_post', 'bb_update_other_excerpt');
200 add_action('save_post', 'bb_update_other_excerpt');
201 add_action('publish_post', 'bb_update_other_excerpt');
202 ?>
Note: See TracBrowser for help on using the browser.