-
Notifications
You must be signed in to change notification settings - Fork 99
fix(NcDateTimePicker): use translated words in dates formatted with a format string #8644
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
Open
odzhychko
wants to merge
1
commit into
main
Choose a base branch
from
NcDateTimePicker-translated-words-in-date-formatted-with-format-string
base: main
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 all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /*! | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: CC0-1.0 | ||
| */ | ||
| import type { Plugin } from 'vite' | ||
|
|
||
| import { exactRegex } from '@rolldown/pluginutils' | ||
| import { glob } from 'fs/promises' | ||
| import { createRequire } from 'module' | ||
| import { dirname, join } from 'path' | ||
|
|
||
| /** | ||
| * This plugin generates a module that can be used to dynamically load locales from `date-fns`. | ||
| * It generates it in a way that it works with the externalization of the `date-fns` locales | ||
| * and with Vite and Webpack apps. | ||
| * | ||
| * A plugin is used because Vite's dynamic imports and glob imports cannot generate imports in such a way. | ||
| */ | ||
| export default () => { | ||
| let localeFiles: string[] = [] | ||
| const virtualModuleId = 'virtual:date-fns-locales' | ||
| const resolvedVirtualModuleId = '\0' + virtualModuleId | ||
| return { | ||
| name: 'date-fns-locales-plugin', | ||
| enforce: 'pre', | ||
| async buildStart() { | ||
| this.info('Collectioning locales in date-fns/locales') | ||
| const require = createRequire(import.meta.url) | ||
| const pkgJsonPath = require.resolve('date-fns/package.json') | ||
| const localeDir = join(dirname(pkgJsonPath), 'locale') | ||
| localeFiles = await Array.fromAsync(glob('*.js', { | ||
| cwd: localeDir, | ||
| exclude: ['types.js', 'en-US.js', 'cdn.js', 'cdn.min.js'], | ||
| })) | ||
| }, | ||
| resolveId: { | ||
| filter: { id: /dateFnsLocaleLoader\.ts$/ }, | ||
| handler() { | ||
| return resolvedVirtualModuleId | ||
| }, | ||
| }, | ||
| load: { | ||
| filter: { id: exactRegex(resolvedVirtualModuleId) }, | ||
| handler() { | ||
| let content = '' | ||
| content += 'const loader = {}\n' | ||
| content += 'export default loader\n' | ||
| for (const localeFile of localeFiles) { | ||
| // e.g., "pt-BR.js" -> "pt-BR" | ||
| const localeCode = localeFile.slice(0, -3) | ||
| let exportName = localeCode.replaceAll('-', '') | ||
| // One locale is named inconsistently... | ||
| if (localeCode === 'be-tarask') { | ||
| exportName = 'beTarasak' | ||
| } | ||
| content += `loader['${localeCode}'] = async () => (await import('date-fns/locale/${localeCode}')).${exportName}\n` | ||
| } | ||
| return content | ||
| }, | ||
| }, | ||
| } as Plugin | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,38 @@ | ||
| /*! | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| /// <reference types="@types/webpack-env" /> | ||
|
|
||
| import type { Locale } from 'date-fns' | ||
|
|
||
| // This is only required for `npm run styleguide`. | ||
| // For `npm run build`, `npm run dev` and everything else using Vite | ||
| // all the code from this file is overriden by `build/date-fns-locales-plugin.mts`. | ||
| const webpackContext = import.meta.webpackContext('../../../node_modules/date-fns/locale', { | ||
| recursive: false, | ||
| regExp: /\.js$/, | ||
| exclude: /(types|en-US|cdn(\.min)?)\.js$/, | ||
| mode: 'lazy', | ||
| }) | ||
|
|
||
| /** | ||
| * Given a `key` load the locale from the webpack context. | ||
| * | ||
| * @param key File name for the locale e.g., ./nl-BE.js' | ||
| */ | ||
| async function loadLocale(key: string) { | ||
| const mod = await webpackContext<Promise<{ default: Locale }>>(key) | ||
| return mod.default | ||
| } | ||
|
|
||
| const loader: Record<string, () => Promise<Locale>> = {} | ||
|
|
||
| for (const key of webpackContext.keys()) { | ||
| // Remove prefix `./` and suffix `.js` to get the locale code. | ||
| const localeCode = key.slice(2, -3) | ||
| loader[localeCode] = () => loadLocale(key) | ||
| } | ||
|
|
||
| export default loader |
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,42 @@ | ||
| /*! | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type { Locale } from 'date-fns' | ||
| import type { ComputedRef, MaybeRefOrGetter } from 'vue' | ||
|
|
||
| import { computedAsync } from '@vueuse/core' | ||
| import { enUS } from 'date-fns/locale/en-US' | ||
| import { computed, toValue } from 'vue' | ||
| import loader from './dateFnsLocaleLoader.ts' | ||
|
|
||
| /** | ||
| * Given a locale try to load the corresponding locale from date-fns. | ||
| * Fall back to en-US while loading, if the request locale does not exist or could not be loaded. | ||
| * | ||
| * @param locale locale code (e.g., 'de-DE') | ||
| */ | ||
| export default function useDateFnsLocale(locale: MaybeRefOrGetter<string>): ComputedRef<Locale> { | ||
| const loadedLocale = computedAsync(() => loadDateFnsLocale(toValue(locale))) | ||
| return computed(() => loadedLocale.value ?? enUS) | ||
| } | ||
|
|
||
| /** | ||
| * Given a locale code load the locale from the loader. | ||
| * | ||
| * @param locale Locale code (e.g, nl-BE.js) | ||
| */ | ||
| async function loadDateFnsLocale(locale: string): Promise<Locale | undefined> { | ||
| if (locale in loader) { | ||
| return await loader[locale]() | ||
| } | ||
|
|
||
| if (locale.includes('-')) { | ||
| // Try without region | ||
| const shortLocale = locale.split('-')[0] | ||
| return loadDateFnsLocale(shortLocale) | ||
| } | ||
|
|
||
| return undefined | ||
| } |
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { createLibConfig } from '@nextcloud/vite-config' | |
| import { globSync } from 'glob' | ||
| import { join, resolve } from 'node:path' | ||
| import { defineConfig } from 'vite' | ||
| import dateFnsLocalesPlugin from './build/date-fns-locales-plugin.mts' | ||
| import vueDocsPlugin from './build/docs-plugin.ts' | ||
| import l10nPlugin from './build/l10n-plugin.mjs' | ||
| import packageJson from './package.json' with { type: 'json' } | ||
|
|
@@ -31,6 +32,7 @@ const entryPoints = { | |
| const overrides = defineConfig({ | ||
| plugins: [ | ||
| vueDocsPlugin, | ||
| dateFnsLocalesPlugin(), | ||
| l10nPlugin(resolve(import.meta.dirname, 'l10n')), | ||
| ], | ||
| css: { | ||
|
|
@@ -71,7 +73,7 @@ export default defineConfig((env) => { | |
| // By default all dependencies are external, but no path imports | ||
| nodeExternalsOptions: { | ||
| // Packages with paths imports should be added here to mark them as external as well | ||
| include: [/^@nextcloud\/.+\//, /^@mdi\/svg\//], | ||
| include: [/^@nextcloud\/.+\//, /^@mdi\/svg\//, /^date-fns\/locale\//], | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Externalized as suggested in #7767 (comment) |
||
| // Make sure to not provide uncompiled vue files as dependencies, this will break unit tests | ||
| exclude: [/\.vue(\?|$)/], | ||
| }, | ||
|
|
||
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.
Generated module looks like this.
Uh oh!
There was an error while loading. Please reload this page.
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.
It tested it with Vite and Rspack apps.