locale.bulk.es6.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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').on('change', function() {
  22. // If the filename is fully the language code or the filename
  23. // ends with a language code, pre-select that one.
  24. const matches = $(this)
  25. .val()
  26. .match(/([^.][.]*)([\w-]+)\.po$/);
  27. if (
  28. matches &&
  29. $langcode.find(`option[value="${matches[2]}"]`).length
  30. ) {
  31. $langcode.val(matches[2]);
  32. }
  33. });
  34. }
  35. },
  36. };
  37. })(jQuery, Drupal);