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