locale.bulk.es6.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @file
  3. * Locale behavior.
  4. */
  5. (function ($, Drupal) {
  6. /**
  7. * Select the language code of an imported file based on its filename.
  8. *
  9. * This only works if the file name ends with "LANGCODE.po".
  10. *
  11. * @type {Drupal~behavior}
  12. *
  13. * @prop {Drupal~behaviorAttach} attach
  14. * Attaches behavior for preselecting language code based on filename.
  15. */
  16. Drupal.behaviors.importLanguageCodeSelector = {
  17. attach(context, settings) {
  18. const $form = $('#locale-translate-import-form').once('autodetect-lang');
  19. if ($form.length) {
  20. const $langcode = $form.find('.langcode-input');
  21. $form.find('.file-import-input')
  22. .on('change', function () {
  23. // If the filename is fully the language code or the filename
  24. // ends with a language code, pre-select that one.
  25. const matches = $(this).val().match(/([^.][.]*)([\w-]+)\.po$/);
  26. if (matches && $langcode.find(`option[value="${matches[2]}"]`).length) {
  27. $langcode.val(matches[2]);
  28. }
  29. });
  30. }
  31. },
  32. };
  33. }(jQuery, Drupal));