-
Notifications
You must be signed in to change notification settings - Fork 12
Add translations to times and numbers #1223
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
Merged
jamiebenstead
merged 10 commits into
translate-web-component
from
add-translations-to-times-and-numbers
Jul 7, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ae47932
Add translation to the last saved text
jamiebenstead 1d26128
Move logic to own file, add in tests
jamiebenstead fb3ae9b
Update changelog
jamiebenstead 29f7b43
Update version of date-fns
jamiebenstead 8012e38
Default to en if no match found
jamiebenstead 2dadcdb
Change distance format from narrow to short
loiswells97 6b7188c
test fixing
loiswells97 2bd2322
Merge branch 'main' into add-translations-to-times-and-numbers
loiswells97 89b59e5
Pass fallbackLng down from i18n
jamiebenstead b342105
Fix typo
jamiebenstead 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| import { intlFormatDistance } from "date-fns"; | ||
|
|
||
| const localeMap = { | ||
| "en-US": "en-US", | ||
| en: "en-GB", | ||
| "fr-FR": "fr", | ||
| "es-LA": "es", | ||
| }; | ||
|
|
||
| export const formatRelativeTime = ( | ||
| lastSavedTime, | ||
| now, | ||
| languageCode, | ||
| fallbackLng, | ||
| ) => { | ||
| const locale = localeMap[languageCode] || fallbackLng; | ||
| return intlFormatDistance(lastSavedTime, now, { | ||
| style: "short", | ||
| locale, | ||
| }); | ||
| }; | ||
This file contains hidden or 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,74 @@ | ||
| import { formatRelativeTime } from "./formatRelativeTime"; | ||
|
|
||
| describe("formatRelativeTime", () => { | ||
| describe("formats the times for now in different locales", () => { | ||
| const now = Date.now(); | ||
|
|
||
| test("formats relative time in en", () => { | ||
| const result = formatRelativeTime(now, now, "en"); | ||
| expect(result).toEqual("now"); | ||
| }); | ||
|
|
||
| test("formats relative time in en-US", () => { | ||
| const result = formatRelativeTime(now, now, "en-US"); | ||
| expect(result).toEqual("now"); | ||
| }); | ||
|
|
||
| test("formats relative time in fr-FR", () => { | ||
| const result = formatRelativeTime(now, now, "fr-FR"); | ||
| expect(result).toEqual("maintenant"); | ||
| }); | ||
|
|
||
| test("formats relative time in es-LA", () => { | ||
| const result = formatRelativeTime(now, now, "es-LA"); | ||
| expect(result).toEqual("ahora"); | ||
| }); | ||
| }); | ||
| describe("formats the times for one minute ago in different locales", () => { | ||
| const oneMinuteAgo = new Date(Date.now() - 60 * 1000); // 1 minute ago | ||
|
|
||
| test("formats relative time in en", () => { | ||
| const result = formatRelativeTime(oneMinuteAgo, Date.now(), "en"); | ||
| expect(result).toEqual("1 min ago"); | ||
| }); | ||
|
|
||
| test("formats relative time in en-US", () => { | ||
| const result = formatRelativeTime(oneMinuteAgo, Date.now(), "en-US"); | ||
| expect(result).toEqual("1 min. ago"); | ||
| }); | ||
|
|
||
| test("formats relative time in fr-FR", () => { | ||
| const result = formatRelativeTime(oneMinuteAgo, Date.now(), "fr-FR"); | ||
| expect(result).toEqual("il y a 1\u00A0min"); | ||
|
danhalson marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| test("formats relative time in es-LA", () => { | ||
| const result = formatRelativeTime(oneMinuteAgo, Date.now(), "es-LA"); | ||
| expect(result).toEqual("hace 1 min"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("formats the times for one hour ago in different locales", () => { | ||
| const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000); // 1 hour ago | ||
|
|
||
| test("formats relative time in en", () => { | ||
| const result = formatRelativeTime(oneHourAgo, Date.now(), "en"); | ||
| expect(result).toEqual("1 hr ago"); | ||
| }); | ||
|
|
||
| test("formats relative time in en-US", () => { | ||
| const result = formatRelativeTime(oneHourAgo, Date.now(), "en-US"); | ||
| expect(result).toEqual("1 hr. ago"); | ||
| }); | ||
|
|
||
| test("formats relative time in fr-FR", () => { | ||
| const result = formatRelativeTime(oneHourAgo, Date.now(), "fr-FR"); | ||
| expect(result).toEqual("il y a 1\u00A0h"); | ||
| }); | ||
|
|
||
| test("formats relative time in es-LA", () => { | ||
| const result = formatRelativeTime(oneHourAgo, Date.now(), "es-LA"); | ||
| expect(result).toEqual("hace 1 h"); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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.
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.
I'm lacking a bit of context here, but are we redefining this list, rather than using a shared source of truth?
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 the answer is that this map is for ensuring that our locales match up with the ones from the library
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.
could we move this into a central location, maybe a separate util called
localeMappings.jsor something, and make sure nowhere else is redefining it?