forked from WordPress/Learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.php
executable file
·163 lines (126 loc) · 3.61 KB
/
i18n.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/php
<?php
namespace WPOrg_Learn\Bin\I18n;
use Requests;
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
const ENDPOINT_BASE = 'https://learn.wordpress.org/wp-json/wp/v2/';
/**
* Get data about taxonomies from a REST API endpoint.
*
* @param array $valid_post_types Slugs of post types that support the taxonomies we want.
*
* @return array
*/
function get_taxonomies( array $valid_post_types = array() ) {
$endpoint = ENDPOINT_BASE . 'taxonomies';
$response = Requests::get( $endpoint );
if ( 200 !== $response->status_code ) {
die( 'Could not retrieve taxonomy data.' );
}
$taxonomies = json_decode( $response->body, true );
if ( ! is_array( $taxonomies ) ) {
die( 'Taxonomies request returned unexpected data.' );
}
if ( count( $valid_post_types ) > 0 ) {
$taxonomies = array_filter(
$taxonomies,
function( $tax ) use ( $valid_post_types ) {
$supported_types = $tax['types'];
$matches = array_intersect( $supported_types, $valid_post_types );
return count( $matches ) > 0;
}
);
}
return $taxonomies;
}
/**
* Get data about a taxonomy's terms from a REST API endpoint.
*
* @param string $taxonomy
*
* @return array
*/
function get_taxonomy_terms( $taxonomy ) {
$endpoint = ENDPOINT_BASE . $taxonomy . '?per_page=100';
$response = Requests::get( $endpoint );
if ( 200 !== $response->status_code ) {
die( sprintf(
'Could not retrieve terms for %s.',
$taxonomy // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
) );
}
$terms = json_decode( $response->body, true );
if ( ! is_array( $terms ) ) {
die( sprintf(
'Terms request for %s returned unexpected data.',
$taxonomy // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
) );
}
return $terms;
}
/**
* Run the script.
*/
function main() {
if ( 'cli' === php_sapi_name() ) {
echo "\n";
echo "Retrieving taxonomies...\n";
}
$valid_post_types = array(
'lesson-plan',
'wporg_workshop',
'course',
'lesson',
);
$taxonomies = get_taxonomies( $valid_post_types );
if ( 'cli' === php_sapi_name() ) {
echo "Retrieving terms...\n";
echo "\n";
}
$terms_by_tax = array();
foreach ( $taxonomies as $taxonomy ) {
$terms = get_taxonomy_terms( $taxonomy['slug'] );
if ( count( $terms ) > 0 ) {
$terms_by_tax[ $taxonomy['name'] ] = $terms;
}
unset( $terms );
}
$file_content = '';
foreach ( $terms_by_tax as $tax_label => $terms ) {
$label = addcslashes( $tax_label, "'" );
foreach ( $terms as $term ) {
$name = addcslashes( $term['name'], "'" );
$link = addcslashes( $term['link'], '*' );
$file_content .= "/* translators: {$link} */\n_x( '{$name}', '$label term name', 'wporg-learn' );\n";
if ( 'cli' === php_sapi_name() ) {
echo "$name\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if ( $term['description'] ) {
$description = addcslashes( $term['description'], "'" );
$file_content .= "/* translators: {$link} */\n_x( '{$description}', '$label term description', 'wporg-learn' );\n";
}
}
}
$path = dirname( __DIR__ ) . '/extra';
if ( ! is_readable( $path ) ) {
mkdir( $path );
}
$file_name = 'translation-strings.php';
$file_header = <<<HEADER
<?php
/**
* Generated file for translation strings.
*
* Used to import additional strings into the learn-wordpress translation project.
*
* ⚠️ This is a generated file. Do not edit manually. See bin/i18n.php.
* ⚠️ Do not require or include this file anywhere.
*/
HEADER;
file_put_contents( $path . '/' . $file_name, $file_header . $file_content );
if ( 'cli' === php_sapi_name() ) {
echo "\n";
echo "Done.\n";
}
}
main();