Skip to:
Content

BuddyPress.org

Ticket #4857: translatecode-003.php

File translatecode-003.php, 3.9 KB (added by DJPaul, 13 years ago)
Line 
1<?php
2// markoheijnen has some props for providing glotpress API examples
3
4/**
5 * Communicates with http://translate.wordpress.org/ and downloads a translation for the current locale
6 *
7 * @since BuddyPress (1.8)
8 */
9class BP_Translate {
10
11 /**
12 * Singleton instance of the BP_Translate class
13 *
14 * @since BuddyPress (1.8)
15 * @var BP_Translate
16 */
17 private static $instance;
18
19 /**
20 * Return the singleton instance of the BP_Translate class
21 *
22 * @return BP_Translate
23 * @since BuddyPress (1.8)
24 */
25 static public function get_instance() {
26 if ( ! self::$instance )
27 self::$instance = new BP_Translate;
28
29 return self::$instance;
30 }
31
32 /**
33 * Constructor
34 *
35 * @since BuddyPress (1.8)
36 */
37 public function __construct() {
38 $this->register_filters();
39 $this->register_actions();
40
41 $this->register_cron(); // Intentionally after filters + actions
42 }
43
44 /**
45 * Hook into filters necessary to automate the translation process
46 *
47 * @since BuddyPress (1.8)
48 */
49 protected function register_filters() {
50 add_filter( 'cron_schedules', array( __CLASS__, 'add_weekly_frequency_to_cron' ) ) ;
51 }
52
53 /**
54 * Hook into actions necessary to automate the translation process
55 *
56 * @since BuddyPress (1.8)
57 */
58 protected function register_actions() {
59 add_action( 'bp_translate_update_check', array( __CLASS__, 'maybe_update_translation' ) );
60 }
61
62 /**
63 * Register cron task to check for language updates
64 *
65 * @since BuddyPress (1.8)
66 */
67 protected function register_cron() {
68 if ( ! wp_next_scheduled( 'bp_translate_update_check' ) )
69 wp_schedule_event( time(), 'bp_weekly', 'bp_translate_update_check' );
70 }
71
72 /**
73 * Adds a "weekly" frequency type to the cron system
74 *
75 * @param array $schedules
76 * @since BuddyPress (1.8)
77 */
78 static public function add_weekly_frequency_to_cron( $schedules ) {
79 $schedules['bp_weekly'] = array(
80 'display' => __( 'Once Weekly', 'buddypress' ),
81 'interval' => WEEK_IN_SECONDS,
82 );
83
84 return $schedules;
85 }
86
87 /**
88 * Find out if there's a newer translation available for this site on translate.wordpress.org
89 *
90 * @since BuddyPress (1.8)
91 */
92 static public function maybe_update_translation() {
93 $locale = apply_filters( 'buddypress_locale', get_locale() );
94
95 // BuddyPress is written in American English as default, so bail out early.
96 if ( 'en_US' == $locale )
97 return;
98
99 // Get the list of available translation from Translate WordPress. This is expected to be JSON.
100 $translations = wp_remote_get( sprintf( 'https://translate.wordpress.org/api/projects/buddypress/%1$s', '1.6.x' ) );
101 if ( is_wp_error( $translations ) || wp_remote_retrieve_response_code( $translations ) !== 200 )
102 return;
103
104 $translations = json_decode( wp_remote_retrieve_body( $translations ) );
105 if ( is_null( $translations ) )
106 return;
107
108 // See if the requested $locale has an available translation
109 $translations = array_shift( wp_list_filter( $translations->translation_sets, array( 'wp_locale' => $locale ) ) );
110 if ( empty( $translations ) )
111 return;
112
113 // Download the .mo
114 $url = sprintf( 'https://translate.wordpress.org/projects/buddypress/%1$s/%2$s/default/export-translations?format=mo', '1.6.x', $translations->locale );
115 $tmp_file = download_url( esc_url_raw( $url ) );
116 if ( is_wp_error( $tmp_file ) )
117 return;
118
119 // Check the language folder exists
120 $dir = WP_LANG_DIR . '/plugins/';
121 if ( ! wp_mkdir_p( $dir ) )
122 return;
123
124 // Move the .mo into place
125 @copy( $tmp_file, sprintf( "{$dir}buddypress-%1\$s.mo", $locale ) );
126 @unlink( $tmp_file );
127 }
128}
129
130// Temporary function name for testing
131function buddypress_translate() {
132
133 // Bail out if we're not in wp-admin, or a site admin
134 if ( ! is_admin() || ! is_super_admin() || ! apply_filters( 'bp_fetch_translations', true ) )
135 return;
136
137 BP_Translate::get_instance();
138
139 /**
140 * @todo This line is just for easy testing. This is meant to be triggered by the cron.
141 */
142 BP_Translate::maybe_update_translation();
143}
144add_action( 'bp_admin_init', 'buddypress_translate' );