-
Notifications
You must be signed in to change notification settings - Fork 141
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
feat: 多言語化対応の基盤を作成 #5220
Draft
Qs-F
wants to merge
12
commits into
master
Choose a base branch
from
feat/multilingualization
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.
+273
−7
Draft
feat: 多言語化対応の基盤を作成 #5220
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
afd4ef5
chore: install react-intl
Qs-F 9779749
chore: add intl-messageformat
Qs-F d5fa649
feat: add IntlProvider
Qs-F 8b0614f
feat: add useIntl hooks
Qs-F fcbd101
feat: add locales configutation
Qs-F 7a23e0b
feat: add Formatter components
Qs-F 5b9c7a4
fix: localize TextLink component
Qs-F 6613c87
fix: tsconfig
Qs-F e35b0c8
fix: change target build ES version
Qs-F f7e327f
feat: add supported languages
Qs-F ffa9c39
refactor: remove unused file
Qs-F 18d4151
Merge remote-tracking branch 'origin' into feat/multilingualization
Qs-F 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
Empty file.
17 changes: 17 additions & 0 deletions
17
packages/smarthr-ui/src/components/Formatter/FormattedMessage.tsx
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,17 @@ | ||
import React, { ComponentProps } from 'react' | ||
import { FormattedMessage as RawFormattedMessage } from 'react-intl' | ||
|
||
import { locale as ja } from '../../locales/ja' | ||
import { Messages } from '../../locales/types' | ||
|
||
type Props<Id extends keyof Messages> = Omit< | ||
ComponentProps<typeof RawFormattedMessage>, | ||
'id' | 'defaultMessage' | ||
> & { | ||
id: Id | ||
defaultMessage: (typeof ja)[Id] | ||
} | ||
|
||
export const FormattedMessage = <ID extends keyof Messages>({ values, ...props }: Props<ID>) => ( | ||
<RawFormattedMessage {...props} values={{ break: <br />, ...values }} /> | ||
) |
22 changes: 22 additions & 0 deletions
22
packages/smarthr-ui/src/components/IntlProvider/IntlProvider.tsx
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,22 @@ | ||
'use client' | ||
|
||
import React, { FC, PropsWithChildren } from 'react' | ||
import { IntlProvider as ReactIntlProvider } from 'react-intl' | ||
|
||
import { locale as ja } from '../../locales/ja' | ||
import { Messages } from '../../locales/types' | ||
|
||
const localeMap = { | ||
ja, | ||
} as const | ||
|
||
type Props = PropsWithChildren<{ | ||
locale: keyof typeof localeMap | ||
messages: Messages | ||
}> | ||
|
||
export const IntlProvider: FC<Props> = ({ locale, children }) => ( | ||
<ReactIntlProvider locale={locale} messages={localeMap[locale]}> | ||
{children} | ||
</ReactIntlProvider> | ||
) |
10 changes: 10 additions & 0 deletions
10
packages/smarthr-ui/src/components/IntlProvider/localeMap.ts
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,10 @@ | ||
export const localeMap = { | ||
ja: '日本語', | ||
'id-id': 'Bahasa Indonesia', | ||
'en-us': 'English', | ||
pt: 'Português', | ||
vi: 'Tiếng Việt', | ||
ko: '한국어', | ||
'zh-cn': '简体中文', | ||
'zh-tw': '繁體中文', | ||
} as const |
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,54 @@ | ||
import { FormatXMLElementFn, Options as IntlMessageFormatOptions } from 'intl-messageformat' | ||
import { useCallback } from 'react' | ||
import { | ||
PrimitiveType, | ||
MessageDescriptor as ReactIntlMessageDescriptor, | ||
useIntl as useReactIntl, | ||
} from 'react-intl' | ||
|
||
import { locale as ja } from '../locales/ja' | ||
import { Messages } from '../locales/types' | ||
|
||
/** | ||
* MEMO: | ||
* src/components/parts/FormattedMessage/FormattedMessage.tsx | ||
* と同じやり方で型を拡張している | ||
*/ | ||
type MessageDescriptor<T extends keyof Messages> = Omit<ReactIntlMessageDescriptor, 'id'> & { | ||
id: T | ||
defaultMessage: (typeof ja)[T] | ||
} | ||
|
||
export const useIntl = () => { | ||
const intl = useReactIntl() | ||
// const lang = useLanguage() | ||
const lang = intl.locale | ||
|
||
const formatMessage = useCallback( | ||
<T extends keyof Messages>( | ||
descriptor: MessageDescriptor<T>, | ||
values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, | ||
opts?: IntlMessageFormatOptions, | ||
): string => intl.formatMessage(descriptor, values, opts), | ||
[intl], | ||
) | ||
|
||
const formatDate = useCallback( | ||
(date: Date, opts?: Intl.DateTimeFormatOptions & { jaFormat?: boolean }): string => { | ||
// 日本語の場合デザインシステム上のフォーマットは「YYYY/MM/DD」形式なので、それに合わせるための対応 | ||
const slashFormat = !opts?.jaFormat && lang === 'ja' | ||
|
||
const overrideOpts: Intl.DateTimeFormatOptions = { | ||
year: 'numeric', | ||
month: slashFormat ? '2-digit' : 'short', | ||
day: '2-digit', | ||
...opts, | ||
} as const | ||
|
||
return intl.formatDate(date, overrideOpts) | ||
}, | ||
[intl, lang], | ||
) | ||
|
||
return { formatMessage, formatDate } | ||
} |
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 @@ | ||
import { Messages } from './types' | ||
|
||
export const locale = { | ||
'smarthr-ui/TextLink/OpenInNewTab': '', | ||
} as const satisfies Messages |
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 @@ | ||
import { Messages } from './types' | ||
|
||
export const locale = { | ||
'smarthr-ui/TextLink/OpenInNewTab': '別タブで開く', | ||
} as const satisfies Messages |
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 type Messages = { | ||
'smarthr-ui/TextLink/OpenInNewTab': string | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.