-
Notifications
You must be signed in to change notification settings - Fork 303
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
Added utility method to resolve locale supplied and fallback #2430
Open
rajkeshwar
wants to merge
5
commits into
ing-bank:master
Choose a base branch
from
rajkeshwar:feature/i18-dynamic-imports
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f79fde7
Added utility method to resolve locale supplied and fallback
rajkeshwar 82d05d9
Added the changeset
rajkeshwar 0c9ea8c
fixed eslint
rajkeshwar 6f43a92
Removed console.log
rajkeshwar 93d2f74
Merge branch 'master' into feature/i18-dynamic-imports
rajkeshwar 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,5 @@ | ||
--- | ||
'@lion/ui': minor | ||
--- | ||
|
||
Added utility method to resolve locale supplied and fallback |
20 changes: 20 additions & 0 deletions
20
packages/ui/components/localize/src/utils/resolveLocaleConfig.js
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,20 @@ | ||
/** | ||
* | ||
* @param {string} namespace Namespace for the locale | ||
* @param {any} localeDir Directory of the locale | ||
* @returns Promise<LocaleConfig> | ||
*/ | ||
export const resolveLocaleConfig = (namespace, localeDir) => ({ | ||
[namespace]: (locale = 'en') => { | ||
const localePath = (/** @type {string} */ loc) => [localeDir, `${loc}.js`].join('/'); | ||
|
||
const fallbackLocale = localePath(locale.replace(/-\w+/, '')); | ||
|
||
return import(localePath(locale)).catch(reason => { | ||
console.warn(` | ||
Locale ${locale} not found. Reason ${reason}. | ||
Loading fallback locale ${fallbackLocale}`); | ||
return import(fallbackLocale); | ||
rajkeshwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}, | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/ui/components/localize/test/resolveLocaleConfig.test.js
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,19 @@ | ||
import { expect } from '@open-wc/testing'; | ||
import { resolveLocaleConfig } from '../src/utils/resolveLocaleConfig.js'; | ||
|
||
describe('resolveLocaleConfig', () => { | ||
it('works correctly', async () => { | ||
const localePath = new URL('./translations', import.meta.url); | ||
const localeConfig = await resolveLocaleConfig('demo', localePath); | ||
|
||
// When no locale is supplied `en.js` is loaded | ||
expect((await localeConfig.demo()).default.hello).to.equal('en.js loaded'); | ||
|
||
// When `de-DE` locale is supplied `de-DE.js` is loaded | ||
expect((await localeConfig.demo('de-DE')).default.hello).to.equal('de-DE.js loaded'); | ||
|
||
// When `de-AB` locale is supplied and the file does not exists. | ||
// Then everything from - is removed and `de.js` is loaded. Which is equivalent to the default case. | ||
expect((await localeConfig.demo('de-AB')).default.hello).to.equal('de.js loaded'); | ||
}); | ||
}); |
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,3 @@ | ||
export default { | ||
hello: 'de-DE.js loaded', | ||
}; |
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,3 @@ | ||
export default { | ||
hello: 'de.js loaded', | ||
}; |
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,3 @@ | ||
export default { | ||
hello: 'en-GB.js loaded', | ||
}; |
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,3 @@ | ||
export default { | ||
hello: 'en.js loaded', | ||
}; |
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.
Can we apply the
resolveLocaleConfig()
to all lion-ui components that have translations?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.
Hi @rajkeshwar.
Don't want to introduce a show stopper, but... keep in mind the reason for the current verbosity is to support all bundlers.
We came from a situation with oneliners like
return import('@lion/ui/combobox-translations/${myLocale}.js')
.Rollup could handle this, Webpack couldn't.
I'm afraid that delegating the dynamic import to an external function makes it harder for all bundlers to make correct chunks.
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.
N.B. it really is a good idea to standardize/simplify the localize code: the current state can definitely be improved.
But maybe we need to do some ready work/discussion together and formulate requirements before we do so. Like:
So maybe it would be good to co-operate with a colleague of us (Tom Nys (unfortunately I don't have his github handle atm)), who is already thinking about above things.
@rajkeshwar maybe you can start this discussion here: https://github.com/ing-bank/lion/discussions
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.
Hi @tlouisse,
Thank you so much for the detailed explanation and for outlining the key areas to consider. To further explore this topic, I’ve started a discussion to gather input on the direction: #2431.