-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2.x manual backport]Change the locale dynamically by adding &i18n-lo…
…cale to URL (#7686) (#7781) Backport PR: #7686 * Change the locale dynamically by adding &i18n-locale to URL The main issue was the inability to dynamically change the locale in OpenSearch Dashboards. Currently we need to update config file and i18nrc.json. This PR allows users to switch to a different locale (e.g., from English to Chinese) by appending or modifying the 'i18n-locale' parameter in the URL. * getAndUpdateLocaleInUrl: If a non-default locale is found, this function reconstructs the URL with the locale parameter in the correct position. * updated the ScopedHistory class, allowing it to detect locale changes and trigger reloads as necessary. * modify the i18nMixin, which sets up the i18n system during server startup, to register all available translation files during server startup, not just the current locale. * update the uiRenderMixin to accept requests for any registered locale and dynamically load and cache translations for requested locales. --------- Signed-off-by: Anan Zhuang <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9c01cff
commit f2faa0a
Showing
14 changed files
with
592 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- Change the locale dynamically by adding &i18n-locale to URL ([#7686](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7686)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { getLocaleInUrl } from './locale_helper'; | ||
|
||
describe('getLocaleInUrl', () => { | ||
beforeEach(() => { | ||
// Clear any warnings before each test | ||
delete (window as any).__localeWarning; | ||
}); | ||
|
||
it('should return the locale from a valid query string', () => { | ||
const url = 'http://localhost:5603/app/home?locale=en-US'; | ||
expect(getLocaleInUrl(url)).toBe('en-US'); | ||
}); | ||
|
||
it('should return the locale from a valid hash query string', () => { | ||
const url = 'http://localhost:5603/app/home#/?locale=fr-FR'; | ||
expect(getLocaleInUrl(url)).toBe('fr-FR'); | ||
}); | ||
|
||
it('should return en for a URL without locale', () => { | ||
const url = 'http://localhost:5603/app/home'; | ||
expect(getLocaleInUrl(url)).toBe('en'); | ||
}); | ||
|
||
it('should return en and set a warning for an invalid locale format in hash', () => { | ||
const url = 'http://localhost:5603/app/home#/&locale=de-DE'; | ||
expect(getLocaleInUrl(url)).toBe('en'); | ||
expect((window as any).__localeWarning).toBeDefined(); | ||
expect((window as any).__localeWarning.title).toBe('Invalid URL Format'); | ||
}); | ||
|
||
it('should return en for an empty locale value', () => { | ||
const url = 'http://localhost:5603/app/home?locale='; | ||
expect(getLocaleInUrl(url)).toBe('en'); | ||
}); | ||
|
||
it('should handle URLs with other query parameters', () => { | ||
const url = 'http://localhost:5603/app/home?param1=value1&locale=ja-JP¶m2=value2'; | ||
expect(getLocaleInUrl(url)).toBe('ja-JP'); | ||
}); | ||
|
||
it('should handle URLs with other hash parameters', () => { | ||
const url = 'http://localhost:5603/app/home#/route?param1=value1&locale=zh-CN¶m2=value2'; | ||
expect(getLocaleInUrl(url)).toBe('zh-CN'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Extracts the locale value from a given URL. | ||
* | ||
* This function looks for the 'locale' parameter in either the main query string | ||
* or in the hash part of the URL. It supports two valid formats: | ||
* 1. As a regular query parameter: "?locale=xx-XX" | ||
* 2. In the hash with a proper query string: "#/?locale=xx-XX" | ||
* | ||
* If an invalid format is detected, it sets a warning message on the window object. | ||
* | ||
* @param url - The URL to extract the locale from | ||
* @returns The locale value if found and valid, or null otherwise | ||
*/ | ||
export function getLocaleInUrl(url: string): string | null { | ||
let urlObject: URL; | ||
// Attempt to parse the URL, return null if invalid | ||
try { | ||
urlObject = new URL(url, window.location.origin); | ||
} catch (error) { | ||
setInvalidUrlWarning(); | ||
return null; | ||
} | ||
|
||
let localeValue: string | null = null; | ||
|
||
// Check for locale in the main query string | ||
if (urlObject.searchParams.has('locale')) { | ||
localeValue = urlObject.searchParams.get('locale'); | ||
} | ||
// Check for locale in the hash, but only if it's in proper query string format | ||
else if (urlObject.hash.includes('?')) { | ||
const hashParams = new URLSearchParams(urlObject.hash.split('?')[1]); | ||
if (hashParams.has('locale')) { | ||
localeValue = hashParams.get('locale'); | ||
} | ||
} | ||
|
||
// Check for non standard query format: | ||
if (localeValue === null && url.includes('&locale=')) { | ||
setInvalidUrlWithLocaleWarning(); | ||
return 'en'; | ||
} | ||
|
||
// Return the locale value if found, or 'en' if not found | ||
return localeValue && localeValue.trim() !== '' ? localeValue : 'en'; | ||
} | ||
|
||
function setInvalidUrlWarning(): void { | ||
(window as any).__localeWarning = { | ||
title: 'Invalid URL Format', | ||
text: 'The provided URL is not in a valid format.', | ||
}; | ||
} | ||
|
||
function setInvalidUrlWithLocaleWarning(): void { | ||
(window as any).__localeWarning = { | ||
title: 'Invalid URL Format', | ||
text: | ||
'The locale parameter is not in a valid URL format. ' + | ||
'Use either "?locale=xx-XX" in the main URL or "#/?locale=xx-XX" in the hash. ' + | ||
'For example: "yourapp.com/page?locale=en-US" or "yourapp.com/page#/?locale=en-US".', | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.