Skip to content

Check existence of the `window` object when setting window translations

Ezekiel Kigbo requested to merge ek-text-util-import-failure into master

What does this MR do and why?

Adds some additional checks for the window object in app/assets/javascripts/locale/index.js.

This prevents Uncaught ReferenceError: window is not defined errors when the i18n methods are imported into a Web worker context.

Screenshots or screen recordings

Screenshot_2023-02-09_at_1.44.48_pm

How to set up and validate locally

  1. Apply this patch
diff --git a/app/assets/javascripts/lib/utils/text_utility.js b/app/assets/javascripts/lib/utils/text_utility.js
index 367180714dfb..9df871cdcfbc 100644
--- a/app/assets/javascripts/lib/utils/text_utility.js
+++ b/app/assets/javascripts/lib/utils/text_utility.js
@@ -1,4 +1,5 @@
 import { isString, memoize } from 'lodash';
+import { sprintf, __ } from '~/locale';
 import { base64ToBuffer, bufferToBase64 } from '~/authentication/webauthn/util';
 import {
   TRUNCATE_WIDTH_DEFAULT_WIDTH,
@@ -525,3 +526,5 @@ export function base64DecodeUnicode(str) {
   const decoder = new TextDecoder('utf8');
   return decoder.decode(base64ToBuffer(str));
 }
+
+export const SOME_I18N_STRING = sprintf(__('this is a %{testStr}'), { testStr: 'test' });
  1. Run the user_toggles_whitespace_changes_spec file
$ rspec ./spec/features/merge_request/user_toggles_whitespace_changes_spec.rb
  1. The test will fail with a window is undefined error message.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Ezekiel Kigbo

Merge request reports