Chris@0: /** Chris@0: * @file Chris@0: * Locale behavior. Chris@0: */ Chris@0: Chris@4: (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@4: $form.find('.file-import-input').on('change', function() { Chris@4: // If the filename is fully the language code or the filename Chris@4: // ends with a language code, pre-select that one. Chris@4: const matches = $(this) Chris@4: .val() Chris@4: .match(/([^.][.]*)([\w-]+)\.po$/); Chris@4: if ( Chris@4: matches && Chris@4: $langcode.find(`option[value="${matches[2]}"]`).length Chris@4: ) { Chris@4: $langcode.val(matches[2]); Chris@4: } Chris@4: }); Chris@0: } Chris@0: }, Chris@0: }; Chris@4: })(jQuery, Drupal);