forked from typecho/typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_pot.php
141 lines (122 loc) · 3.28 KB
/
build_pot.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
<?php
$langs = [];
/**
* output lang
*
* @param string $str
*/
function output_lang($str) {
global $langs;
$key = md5($str);
if (!isset($langs[$key])) {
echo $str;
$langs[$key] = true;
}
}
/**
* get all files
*
* @param string $dir
* @param string $pattern
* @return array
*/
function all_files($dir, $pattern = '*') {
$result = array();
$items = glob($dir . '/' . $pattern, GLOB_BRACE);
foreach ($items as $item) {
if (is_file($item)) {
$result[] = $item;
}
}
$items = glob($dir . '/*', GLOB_ONLYDIR);
foreach ($items as $item) {
if (is_dir($item)) {
$result = array_merge($result, all_files($item, $pattern));
}
}
return $result;
}
/**
* get msgid
*
* @param string $value
* @return string
*/
function get_msgid($value) {
if ($value[0] == '"') {
return $value;
} else {
$value = trim($value, "'");
return '"' . str_replace('"', '\"', $value) . '"';
}
}
/**
* get pot from file
*
* @param string $file
* @return string
*/
function get_pot($file) {
$source = file_get_contents($file);
$matched = null;
$plural = [];
foreach (token_get_all($source) as $token) {
if (is_array($token)) {
list ($type, $value) = $token;
if ($type == T_STRING && in_array($value, ['_t', '_e', '_n'])) {
$matched = $value;
} else if ($type == T_CONSTANT_ENCAPSED_STRING && $matched) {
$key = md5($value);
if ($matched == '_n') {
$plural[] = $value;
} else {
output_lang('msgid ' . get_msgid($value) . "\nmsgstr \"\"\n\n");
$matched = null;
}
} else if ($type != T_WHITESPACE) {
$matched = null;
if (!empty($plural)) {
$msgstr = '';
$lang = '';
foreach ($plural as $key => $value) {
$lang .= 'msgid' . ($key == 0 ? '' : '_plural') . ' ' . get_msgid($value) . "\n";
$msgstr .= "msgstr[{$key}] \"\"\n";
}
$lang .= $msgstr . "\n";
output_lang($lang);
$plural = [];
}
}
} else if ($token != ',' && $token != '(') {
$matched = null;
$plural = [];
}
}
}
require_once __DIR__ . '/../var/Typecho/Common.php';
$version = Typecho_Common::VERSION;
$date = date('Y-m-d H:iO');
$year = date('Y');
echo <<<EOF
# Copyright (C) {$year} Typecho
# This file is distributed under the same license as the Typecho Project.
# FIRST AUTHOR <[email protected]>, {$year}.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: {$version}\\n"
"Report-Msgid-Bugs-To: \\n"
"POT-Creation-Date: {$date}\\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: Typecho Dev <[email protected]>\\n"
"Language: \\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"\n\n
EOF;
foreach (all_files(__DIR__ . '/../', "*.php") as $file) {
get_pot($file);
}