Chris@0: /** Chris@0: * @file Chris@0: * Locale behavior. Chris@0: */ Chris@0: Chris@0: (function ($, Drupal) { Chris@0: /** Chris@0: * Select the language code of an imported file based on its filename. Chris@0: * Chris@0: * This only works if the file name ends with "LANGCODE.po". Chris@0: * Chris@0: * @type {Drupal~behavior} Chris@0: * Chris@0: * @prop {Drupal~behaviorAttach} attach Chris@0: * Attaches behavior for preselecting language code based on filename. Chris@0: */ Chris@0: Drupal.behaviors.importLanguageCodeSelector = { Chris@0: attach(context, settings) { Chris@0: const $form = $('#locale-translate-import-form').once('autodetect-lang'); Chris@0: if ($form.length) { Chris@0: const $langcode = $form.find('.langcode-input'); Chris@0: $form.find('.file-import-input') Chris@0: .on('change', function () { Chris@0: // If the filename is fully the language code or the filename Chris@0: // ends with a language code, pre-select that one. Chris@0: const matches = $(this).val().match(/([^.][\.]*)([\w-]+)\.po$/); Chris@0: if (matches && $langcode.find(`option[value="${matches[2]}"]`).length) { Chris@0: $langcode.val(matches[2]); Chris@0: } Chris@0: }); Chris@0: } Chris@0: }, Chris@0: }; Chris@0: }(jQuery, Drupal));