locale.install 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. 'default' => 0, // LOCALE_NOT_CUSTOMIZED
  107. 'description' => 'Boolean indicating whether the translation is custom to this site.',
  108. ],
  109. ],
  110. 'primary key' => ['language', 'lid'],
  111. 'foreign keys' => [
  112. 'locales_source' => [
  113. 'table' => 'locales_source',
  114. 'columns' => ['lid' => 'lid'],
  115. ],
  116. ],
  117. 'indexes' => [
  118. 'lid' => ['lid'],
  119. ],
  120. ];
  121. $schema['locales_location'] = [
  122. 'description' => 'Location information for source strings.',
  123. 'fields' => [
  124. 'lid' => [
  125. 'type' => 'serial',
  126. 'not null' => TRUE,
  127. 'description' => 'Unique identifier of this location.',
  128. ],
  129. 'sid' => [
  130. 'type' => 'int',
  131. 'not null' => TRUE,
  132. 'description' => 'Unique identifier of this string.',
  133. ],
  134. 'type' => [
  135. 'type' => 'varchar_ascii',
  136. 'length' => 50,
  137. 'not null' => TRUE,
  138. 'default' => '',
  139. 'description' => 'The location type (file, config, path, etc).',
  140. ],
  141. 'name' => [
  142. 'type' => 'varchar',
  143. 'length' => 255,
  144. 'not null' => TRUE,
  145. 'default' => '',
  146. 'description' => 'Type dependent location information (file name, path, etc).',
  147. ],
  148. 'version' => [
  149. 'type' => 'varchar_ascii',
  150. 'length' => 20,
  151. 'not null' => TRUE,
  152. 'default' => 'none',
  153. 'description' => 'Version of Drupal where the location was found.',
  154. ],
  155. ],
  156. 'primary key' => ['lid'],
  157. 'foreign keys' => [
  158. 'locales_source' => [
  159. 'table' => 'locales_source',
  160. 'columns' => ['sid' => 'lid'],
  161. ],
  162. ],
  163. 'indexes' => [
  164. 'string_id' => ['sid'],
  165. 'string_type' => ['sid', 'type'],
  166. ],
  167. ];
  168. $schema['locale_file'] = [
  169. 'description' => 'File import status information for interface translation files.',
  170. 'fields' => [
  171. 'project' => [
  172. 'type' => 'varchar_ascii',
  173. 'length' => '255',
  174. 'not null' => TRUE,
  175. 'default' => '',
  176. 'description' => 'A unique short name to identify the project the file belongs to.',
  177. ],
  178. 'langcode' => [
  179. 'type' => 'varchar_ascii',
  180. 'length' => '12',
  181. 'not null' => TRUE,
  182. 'default' => '',
  183. 'description' => 'Language code of this translation. References {language}.langcode.',
  184. ],
  185. 'filename' => [
  186. 'type' => 'varchar',
  187. 'length' => 255,
  188. 'not null' => TRUE,
  189. 'default' => '',
  190. 'description' => 'Filename of the imported file.',
  191. ],
  192. 'version' => [
  193. 'type' => 'varchar',
  194. 'length' => '128',
  195. 'not null' => TRUE,
  196. 'default' => '',
  197. 'description' => 'Version tag of the imported file.',
  198. ],
  199. 'uri' => [
  200. 'type' => 'varchar',
  201. 'length' => 255,
  202. 'not null' => TRUE,
  203. 'default' => '',
  204. 'description' => 'URI of the remote file, the resulting local file or the locally imported file.',
  205. ],
  206. 'timestamp' => [
  207. 'type' => 'int',
  208. 'not null' => FALSE,
  209. 'default' => 0,
  210. 'description' => 'Unix timestamp of the imported file.',
  211. ],
  212. 'last_checked' => [
  213. 'type' => 'int',
  214. 'not null' => FALSE,
  215. 'default' => 0,
  216. 'description' => 'Unix timestamp of the last time this translation was confirmed to be the most recent release available.',
  217. ],
  218. ],
  219. 'primary key' => ['project', 'langcode'],
  220. ];
  221. return $schema;
  222. }
  223. /**
  224. * Implements hook_requirements().
  225. */
  226. function locale_requirements($phase) {
  227. $requirements = [];
  228. if ($phase == 'runtime') {
  229. $available_updates = [];
  230. $untranslated = [];
  231. $languages = locale_translatable_language_list();
  232. if ($languages) {
  233. // Determine the status of the translation updates per language.
  234. $status = locale_translation_get_status();
  235. if ($status) {
  236. foreach ($status as $project) {
  237. foreach ($project as $langcode => $project_info) {
  238. if (empty($project_info->type)) {
  239. $untranslated[$langcode] = $languages[$langcode]->getName();
  240. }
  241. elseif ($project_info->type == LOCALE_TRANSLATION_LOCAL || $project_info->type == LOCALE_TRANSLATION_REMOTE) {
  242. $available_updates[$langcode] = $languages[$langcode]->getName();
  243. }
  244. }
  245. }
  246. if ($available_updates || $untranslated) {
  247. if ($available_updates) {
  248. $requirements['locale_translation'] = [
  249. 'title' => 'Translation update status',
  250. 'value' => \Drupal::l(t('Updates available'), new Url('locale.translate_status')),
  251. 'severity' => REQUIREMENT_WARNING,
  252. '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')]),
  253. ];
  254. }
  255. else {
  256. $requirements['locale_translation'] = [
  257. 'title' => 'Translation update status',
  258. 'value' => t('Missing translations'),
  259. 'severity' => REQUIREMENT_INFO,
  260. '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')]),
  261. ];
  262. }
  263. }
  264. else {
  265. $requirements['locale_translation'] = [
  266. 'title' => 'Translation update status',
  267. 'value' => t('Up to date'),
  268. 'severity' => REQUIREMENT_OK,
  269. ];
  270. }
  271. }
  272. else {
  273. $requirements['locale_translation'] = [
  274. 'title' => 'Translation update status',
  275. 'value' => \Drupal::l(t('Can not determine status'), new Url('locale.translate_status')),
  276. 'severity' => REQUIREMENT_WARNING,
  277. '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')]),
  278. ];
  279. }
  280. }
  281. }
  282. return $requirements;
  283. }
  284. /**
  285. * Delete translation status data in state.
  286. */
  287. function locale_update_8300() {
  288. // Delete the old translation status data, it will be rebuilt and stored in
  289. // the new key value collection.
  290. \Drupal::state()->delete('locale.translation_status');
  291. }