locale.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Locale module.
  5. */
  6. use Drupal\Core\File\Exception\FileException;
  7. use Drupal\Core\File\FileSystemInterface;
  8. use Drupal\Core\Link;
  9. use Drupal\Core\Url;
  10. /**
  11. * Implements hook_install().
  12. */
  13. function locale_install() {
  14. // Create the interface translations directory and ensure it's writable.
  15. if (!$directory = \Drupal::config('locale.settings')->get('translation.path')) {
  16. $site_path = \Drupal::service('site.path');
  17. $directory = $site_path . '/files/translations';
  18. \Drupal::configFactory()->getEditable('locale.settings')->set('translation.path', $directory)->save();
  19. }
  20. \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  21. }
  22. /**
  23. * Implements hook_uninstall().
  24. */
  25. function locale_uninstall() {
  26. $config = \Drupal::config('locale.settings');
  27. // Delete all JavaScript translation files.
  28. $locale_js_directory = 'public://' . $config->get('javascript.directory');
  29. if (is_dir($locale_js_directory)) {
  30. $locale_javascripts = \Drupal::state()->get('locale.translation.javascript') ?: [];
  31. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  32. $file_system = \Drupal::service('file_system');
  33. foreach ($locale_javascripts as $langcode => $file_suffix) {
  34. if (!empty($file_suffix)) {
  35. try {
  36. $file_system->delete($locale_js_directory . '/' . $langcode . '_' . $file_suffix . '.js');
  37. }
  38. catch (FileException $e) {
  39. // Ignore and continue.
  40. }
  41. }
  42. }
  43. // Delete the JavaScript translations directory if empty.
  44. if (is_dir($locale_js_directory)) {
  45. if (!$file_system->scanDirectory($locale_js_directory, '/.*/')) {
  46. $file_system->rmdir($locale_js_directory);
  47. }
  48. }
  49. }
  50. // Clear variables.
  51. \Drupal::state()->delete('system.javascript_parsed');
  52. \Drupal::state()->delete('locale.translation.plurals');
  53. \Drupal::state()->delete('locale.translation.javascript');
  54. }
  55. /**
  56. * Implements hook_schema().
  57. */
  58. function locale_schema() {
  59. $schema['locales_source'] = [
  60. 'description' => 'List of English source strings.',
  61. 'fields' => [
  62. 'lid' => [
  63. 'type' => 'serial',
  64. 'not null' => TRUE,
  65. 'description' => 'Unique identifier of this string.',
  66. ],
  67. 'source' => [
  68. 'type' => 'text',
  69. 'mysql_type' => 'blob',
  70. 'not null' => TRUE,
  71. 'description' => 'The original string in English.',
  72. ],
  73. 'context' => [
  74. 'type' => 'varchar_ascii',
  75. 'length' => 255,
  76. 'not null' => TRUE,
  77. 'default' => '',
  78. 'description' => 'The context this string applies to.',
  79. ],
  80. 'version' => [
  81. 'type' => 'varchar_ascii',
  82. 'length' => 20,
  83. 'not null' => TRUE,
  84. 'default' => 'none',
  85. 'description' => 'Version of Drupal where the string was last used (for locales optimization).',
  86. ],
  87. ],
  88. 'primary key' => ['lid'],
  89. 'indexes' => [
  90. 'source_context' => [['source', 30], 'context'],
  91. ],
  92. ];
  93. $schema['locales_target'] = [
  94. 'description' => 'Stores translated versions of strings.',
  95. 'fields' => [
  96. 'lid' => [
  97. 'type' => 'int',
  98. 'not null' => TRUE,
  99. 'default' => 0,
  100. 'description' => 'Source string ID. References {locales_source}.lid.',
  101. ],
  102. 'translation' => [
  103. 'type' => 'text',
  104. 'mysql_type' => 'blob',
  105. 'not null' => TRUE,
  106. 'description' => 'Translation string value in this language.',
  107. ],
  108. 'language' => [
  109. 'type' => 'varchar_ascii',
  110. 'length' => 12,
  111. 'not null' => TRUE,
  112. 'default' => '',
  113. 'description' => 'Language code. References {language}.langcode.',
  114. ],
  115. 'customized' => [
  116. 'type' => 'int',
  117. 'not null' => TRUE,
  118. // LOCALE_NOT_CUSTOMIZED
  119. 'default' => 0,
  120. 'description' => 'Boolean indicating whether the translation is custom to this site.',
  121. ],
  122. ],
  123. 'primary key' => ['language', 'lid'],
  124. 'foreign keys' => [
  125. 'locales_source' => [
  126. 'table' => 'locales_source',
  127. 'columns' => ['lid' => 'lid'],
  128. ],
  129. ],
  130. 'indexes' => [
  131. 'lid' => ['lid'],
  132. ],
  133. ];
  134. $schema['locales_location'] = [
  135. 'description' => 'Location information for source strings.',
  136. 'fields' => [
  137. 'lid' => [
  138. 'type' => 'serial',
  139. 'not null' => TRUE,
  140. 'description' => 'Unique identifier of this location.',
  141. ],
  142. 'sid' => [
  143. 'type' => 'int',
  144. 'not null' => TRUE,
  145. 'description' => 'Unique identifier of this string.',
  146. ],
  147. 'type' => [
  148. 'type' => 'varchar_ascii',
  149. 'length' => 50,
  150. 'not null' => TRUE,
  151. 'default' => '',
  152. 'description' => 'The location type (file, config, path, etc).',
  153. ],
  154. 'name' => [
  155. 'type' => 'varchar',
  156. 'length' => 255,
  157. 'not null' => TRUE,
  158. 'default' => '',
  159. 'description' => 'Type dependent location information (file name, path, etc).',
  160. ],
  161. 'version' => [
  162. 'type' => 'varchar_ascii',
  163. 'length' => 20,
  164. 'not null' => TRUE,
  165. 'default' => 'none',
  166. 'description' => 'Version of Drupal where the location was found.',
  167. ],
  168. ],
  169. 'primary key' => ['lid'],
  170. 'foreign keys' => [
  171. 'locales_source' => [
  172. 'table' => 'locales_source',
  173. 'columns' => ['sid' => 'lid'],
  174. ],
  175. ],
  176. 'indexes' => [
  177. 'string_id' => ['sid'],
  178. 'string_type' => ['sid', 'type'],
  179. ],
  180. ];
  181. $schema['locale_file'] = [
  182. 'description' => 'File import status information for interface translation files.',
  183. 'fields' => [
  184. 'project' => [
  185. 'type' => 'varchar_ascii',
  186. 'length' => '255',
  187. 'not null' => TRUE,
  188. 'default' => '',
  189. 'description' => 'A unique short name to identify the project the file belongs to.',
  190. ],
  191. 'langcode' => [
  192. 'type' => 'varchar_ascii',
  193. 'length' => '12',
  194. 'not null' => TRUE,
  195. 'default' => '',
  196. 'description' => 'Language code of this translation. References {language}.langcode.',
  197. ],
  198. 'filename' => [
  199. 'type' => 'varchar',
  200. 'length' => 255,
  201. 'not null' => TRUE,
  202. 'default' => '',
  203. 'description' => 'Filename of the imported file.',
  204. ],
  205. 'version' => [
  206. 'type' => 'varchar',
  207. 'length' => '128',
  208. 'not null' => TRUE,
  209. 'default' => '',
  210. 'description' => 'Version tag of the imported file.',
  211. ],
  212. 'uri' => [
  213. 'type' => 'varchar',
  214. 'length' => 255,
  215. 'not null' => TRUE,
  216. 'default' => '',
  217. 'description' => 'URI of the remote file, the resulting local file or the locally imported file.',
  218. ],
  219. 'timestamp' => [
  220. 'type' => 'int',
  221. 'not null' => FALSE,
  222. 'default' => 0,
  223. 'description' => 'Unix timestamp of the imported file.',
  224. ],
  225. 'last_checked' => [
  226. 'type' => 'int',
  227. 'not null' => FALSE,
  228. 'default' => 0,
  229. 'description' => 'Unix timestamp of the last time this translation was confirmed to be the most recent release available.',
  230. ],
  231. ],
  232. 'primary key' => ['project', 'langcode'],
  233. ];
  234. return $schema;
  235. }
  236. /**
  237. * Implements hook_requirements().
  238. */
  239. function locale_requirements($phase) {
  240. $requirements = [];
  241. if ($phase == 'runtime') {
  242. $available_updates = [];
  243. $untranslated = [];
  244. $languages = locale_translatable_language_list();
  245. if ($languages) {
  246. // Determine the status of the translation updates per language.
  247. $status = locale_translation_get_status();
  248. if ($status) {
  249. foreach ($status as $project) {
  250. foreach ($project as $langcode => $project_info) {
  251. if (empty($project_info->type)) {
  252. $untranslated[$langcode] = $languages[$langcode]->getName();
  253. }
  254. elseif ($project_info->type == LOCALE_TRANSLATION_LOCAL || $project_info->type == LOCALE_TRANSLATION_REMOTE) {
  255. $available_updates[$langcode] = $languages[$langcode]->getName();
  256. }
  257. }
  258. }
  259. if ($available_updates || $untranslated) {
  260. if ($available_updates) {
  261. $requirements['locale_translation'] = [
  262. 'title' => t('Translation update status'),
  263. 'value' => Link::fromTextAndUrl(t('Updates available'), Url::fromRoute('locale.translate_status'))->toString(),
  264. 'severity' => REQUIREMENT_WARNING,
  265. 'description' => t('Updates available for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', ['@languages' => implode(', ', $available_updates), ':updates' => Url::fromRoute('locale.translate_status')->toString()]),
  266. ];
  267. }
  268. else {
  269. $requirements['locale_translation'] = [
  270. 'title' => t('Translation update status'),
  271. 'value' => t('Missing translations'),
  272. 'severity' => REQUIREMENT_INFO,
  273. 'description' => t('Missing translations for: @languages. See the <a href=":updates">Available translation updates</a> page for more information.', ['@languages' => implode(', ', $untranslated), ':updates' => Url::fromRoute('locale.translate_status')->toString()]),
  274. ];
  275. }
  276. }
  277. else {
  278. $requirements['locale_translation'] = [
  279. 'title' => t('Translation update status'),
  280. 'value' => t('Up to date'),
  281. 'severity' => REQUIREMENT_OK,
  282. ];
  283. }
  284. }
  285. else {
  286. $requirements['locale_translation'] = [
  287. 'title' => t('Translation update status'),
  288. 'value' => Link::fromTextAndUrl(t('Can not determine status'), Url::fromRoute('locale.translate_status'))->toString(),
  289. 'severity' => REQUIREMENT_WARNING,
  290. 'description' => t('No translation status is available. See the <a href=":updates">Available translation updates</a> page for more information.', [':updates' => Url::fromRoute('locale.translate_status')->toString()]),
  291. ];
  292. }
  293. }
  294. }
  295. return $requirements;
  296. }
  297. /**
  298. * Delete translation status data in state.
  299. */
  300. function locale_update_8300() {
  301. // Delete the old translation status data, it will be rebuilt and stored in
  302. // the new key value collection.
  303. \Drupal::state()->delete('locale.translation_status');
  304. }
  305. /**
  306. * Update default server pattern value to use https.
  307. */
  308. function locale_update_8500() {
  309. $update_url = \Drupal::config('locale.settings')->get('translation.default_server_pattern');
  310. if ($update_url == 'http://ftp.drupal.org/files/translations/%core/%project/%project-%version.%language.po') {
  311. \Drupal::configFactory()->getEditable('locale.settings')
  312. ->set('translation.default_server_pattern', 'https://ftp.drupal.org/files/translations/%core/%project/%project-%version.%language.po')
  313. ->save();
  314. }
  315. }
  316. /**
  317. * Clear Locale project storage to use new 'all' instead of 8.x in URLs.
  318. */
  319. function locale_update_8800() {
  320. \Drupal::service('locale.project')->deleteAll();
  321. }