locale.install 9.8 KB

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