-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi18n.install
203 lines (183 loc) · 5.93 KB
/
i18n.install
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* @file
* Installation file for Internationalization (i18n) module.
*/
/**
* Implements hook_install().
*/
function i18n_install() {
// Set module weight for it to run after core modules.
db_query("UPDATE {system} SET weight = 10 WHERE name = 'i18n' AND type = 'module'");
}
/**
* Add fields to the schema if they don't exist.
*
* @param string $table
* The name of the database table.
* @param array $fields
* The list of database fields to create.
* @param bool $force_rebuild_schema
* Whether to force backdrop_get_schema() to rebuild the schema. This may be
* necessary when additional implementations of hook_schema_alter() have
* become available since the schema was originally built.
*/
function i18n_install_create_fields($table, array $fields, $force_rebuild_schema = FALSE) {
static $schema;
$rebuild_schema = !isset($schema) || $force_rebuild_schema;
$schema = backdrop_get_schema($table, $rebuild_schema);
foreach ($fields as $field) {
if (!empty($schema['fields'][$field])) {
if (!db_field_exists($table, $field)) {
db_add_field($table, $field, $schema['fields'][$field]);
}
else {
// The field exists, make sure field definition is up to date.
db_change_field($table, $field, $field, $schema['fields'][$field]);
}
}
}
}
/**
* Implements hook_update_dependencies().
*/
function i18n_update_dependencies() {
// Ensure conversion to config has already happened.
$dependencies['i18n'][1002] = array(
'user' => 1017,
);
$dependencies['i18n'][1003] = array(
'block' => 1004,
);
return $dependencies;
}
/**
* Implements hook_update_last_removed().
*/
function i18n_update_last_removed() {
return 7001;
}
/**
* Empty hook.
*/
function i18n_update_1000() {
// This hook formerly contained dummy code, which got removed.
}
/**
* Disable unavailable and deprecated modules.
*/
function i18n_update_1001() {
// The following modules don't exist for Backdrop. We disable them by removing
// the rows from the system table.
$obsolete = array(
'variable',
'variable_realm',
'variable_store',
'i18n_block',
'i18n_user',
'i18n_variable',
);
db_delete('system')->condition('name', $obsolete, 'IN')->execute();
}
/**
* Get translatables from variable_store and convert to locale.
*/
function i18n_update_1002() {
if (!db_table_exists('variable_store')) {
return;
}
require_once BACKDROP_ROOT . '/core/modules/config/config.module';
foreach (config_get_info() as $prefix => $info) {
$config = config($prefix);
if ($translatables = $config->get('_config_translatables')) {
foreach ($translatables as $translatable) {
$context = 'config:' . $prefix . ':' . $translatable;
// Trigger locale to create an entry in locales_source and get the lid.
$t = locale($config->get($translatable), $context);
$lid = db_select('locales_source', 'ls')
->fields('ls', array('lid'))
->condition('ls.context', $context, '=')
->orderBy('lid', 'DESC')
->execute()
->fetchField();
if (empty($lid)) {
continue;
}
// Get translations from variable_store for all languages, skip English
// as locale always assumes it to be the source. Variable name and
// config key sometimes differ (prefix), so we need a fuzzy search.
$translations = db_select('variable_store', 'vs')
->fields('vs', array('realm_key', 'value'))
->condition('vs.name', '%' . db_like($translatable), 'LIKE')
->condition('vs.realm_key', 'en', '!=')
->condition('vs.serialized', '0')
->execute()
->fetchAll();
// Create entries in locales_target for each language.
foreach ($translations as $translation) {
db_insert('locales_target')
->fields(array(
'lid' => $lid,
'translation' => $translation->value,
'language' => $translation->realm_key,
))->execute();
}
}
}
}
}
/**
* Get custom block translations and convert to config.
*/
function i18n_update_1003() {
if (!module_exists('i18n_string')) {
// If the submodule i18n_string isn't enabled, no block translations exist.
// We can skip that step.
return;
}
$block_names = config_get_names_with_prefix('block.custom');
$languages = config_get('language.settings', 'languages');
unset($languages['en']);
foreach ($block_names as $block_name) {
$config = config($block_name);
// Locale always assumes English as source.
$config->set('default_langcode', 'en');
foreach (array_keys($languages) as $langcode) {
$translated_title = locale($config->get('title'), 'blocks:block:' . $config->get('delta') . ':title', $langcode);
$translated_body = locale($config->get('body.value'), 'blocks:block:' . $config->get('delta') . ':body', $langcode);
// Save translations for each language.
$config->set('translations.' . $langcode, array(
'info' => $config->get('info'),
'description' => $config->get('description'),
'title' => $translated_title,
'body' => array(
'value' => $translated_body,
'format' => $config->get('body.format'),
),
));
}
$config->save();
}
// Entries in locales_source and locales_target are no longer needed, cleanup.
$result = db_select('locales_source', 's')
->fields('s', array('lid'))
->condition('textgroup', 'blocks')
->execute()
->fetchAllAssoc('lid');
$lids = array_keys($result);
if (empty($lids)) {
return;
}
db_delete('locales_source')
->condition('lid', $lids, 'IN')->execute();
db_delete('locales_target')
->condition('lid', $lids, 'IN')->execute();
}
/**
* Drop obsolete table 'i18n_block_language' if it exists.
*/
function i18n_update_1004() {
if (db_table_exists('i18n_block_language')) {
db_drop_table('i18n_block_language');
}
}