locale.bulk.js 1.1 KB

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