-
Notifications
You must be signed in to change notification settings - Fork 919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change the locale dynamically by adding &i18n-locale to URL #7686
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a0ff79
Change the locale dynamically by adding &i18n-locale to URL
ananzh 4fb00b2
fix PR comments
ananzh 2052a66
fix comments 2
ananzh 77eff19
fix tests
ananzh cea33d4
Changeset file for PR #7686 created/updated
opensearch-changeset-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it common to store this in window?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not uncommon, especially in situations where we need to pass information from sever side to browser that doesn't have a direct communication channel.