-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
202 changed files
with
4,226 additions
and
88 deletions.
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
dist | ||
|
||
node_modules | ||
.yarn/* | ||
!.yarn/cache | ||
!.yarn/patches | ||
|
Binary file not shown.
Binary file added
BIN
+17.5 KB
.yarn/cache/@babel-helper-validator-identifier-npm-7.22.20-18305bb306-136412784d.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+166 KB
.yarn/cache/@formatjs-ecma402-abstract-npm-1.17.2-55bbb7bd5f-026382b1fb.zip
Binary file not shown.
Binary file added
BIN
+4.83 KB
.yarn/cache/@formatjs-fast-memoize-npm-2.2.0-4a46a61b8b-8697fe72a7.zip
Binary file not shown.
Binary file added
BIN
+64.2 KB
.yarn/cache/@formatjs-icu-messageformat-parser-npm-2.6.2-474e5fdc0c-c339a71249.zip
Binary file not shown.
Binary file added
BIN
+16.2 KB
.yarn/cache/@formatjs-icu-skeleton-parser-npm-1.6.2-edd4781caa-f51658d0b0.zip
Binary file not shown.
Binary file added
BIN
+30.1 KB
.yarn/cache/@formatjs-intl-localematcher-npm-0.4.2-18c9cd92d8-df579d8c45.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+14.7 KB
.yarn/cache/@jridgewell-sourcemap-codec-npm-1.4.15-a055fb62cf-b881c7e503.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1000 KB
.yarn/cache/@testing-library-react-npm-14.0.0-84fecd033b-4a54c8f56c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+5.75 KB
.yarn/cache/console-control-strings-npm-1.1.0-e3160e5275-8755d76787.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+89.3 KB
.yarn/cache/dom-accessibility-api-npm-0.5.16-d3e2310666-005eb283ca.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+4.84 KB
.yarn/cache/html-encoding-sniffer-npm-3.0.0-daac3dfe41-8d806aa004.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+2.88 KB
.yarn/cache/is-potential-custom-element-name-npm-1.0.1-f352f606f8-ced7bbbb64.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+5.92 KB
.yarn/cache/stop-iteration-iterator-npm-1.0.0-ea451e1609-d04173690b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 was deleted.
Oops, something went wrong.
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 @@ | ||
import React from 'react'; | ||
|
||
import { type IntlMessage } from './types/intl-message'; | ||
|
||
export type LitIntlContextValue = { | ||
message: IntlMessage; | ||
locale?: string; | ||
}; | ||
|
||
export const LitIntlContext = React.createContext<LitIntlContextValue | undefined>(undefined); |
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,7 @@ | ||
import { LitIntlContext, type LitIntlContextValue } from './lit-intl.context'; | ||
|
||
type LitIntlProviderProps = React.PropsWithChildren<LitIntlContextValue>; | ||
|
||
export function LitIntlProvider({ children, locale, message }: LitIntlProviderProps) { | ||
return <LitIntlContext.Provider value={{ message, locale }}>{children}</LitIntlContext.Provider>; | ||
} |
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 IntlMessage = { | ||
[id: string]: IntlMessage | string; | ||
}; |
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 type React from 'react'; | ||
|
||
import { type PrimitiveType } from 'intl-messageformat'; | ||
|
||
export type TranslationValue = Record<string, PrimitiveType | React.ReactNode>; |
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,69 @@ | ||
import React from 'react'; | ||
|
||
import { LitIntlContext } from './lit-intl.context'; | ||
import { type IntlMessage } from './types/intl-message'; | ||
import { type TranslationValue } from './types/translation'; | ||
|
||
import IntlMessageFormat from 'intl-messageformat'; | ||
|
||
function resolvePath(messages: IntlMessage, idPath: string) { | ||
let message = messages; | ||
|
||
idPath.split('.').forEach((part) => { | ||
const next = message[part] as IntlMessage; | ||
|
||
if (part == null || next == null) { | ||
throw new Error( | ||
`Could not resolve \`${idPath}\` in \`${JSON.stringify(messages, null, 2)}\`.`, | ||
); | ||
} | ||
|
||
message = next; | ||
}); | ||
|
||
return message; | ||
} | ||
|
||
export function useTranslation(path?: string) { | ||
const context = React.useContext(LitIntlContext); | ||
const cachedFormatByLocaleRef = React.useRef<Record<string, Record<string, IntlMessageFormat>>>( | ||
{}, | ||
); | ||
|
||
if (context == null) { | ||
throw new Error(); | ||
} | ||
|
||
const { message: allMessage, locale = 'not-locale' } = context; | ||
const intlMessage = React.useMemo(() => { | ||
if (path == null) return allMessage; | ||
return resolvePath(allMessage, path); | ||
}, [allMessage, path]); | ||
|
||
if (intlMessage == null) { | ||
throw new Error(); | ||
} | ||
|
||
return (idPath: string, value?: TranslationValue) => { | ||
const cachedFormatByLocale = cachedFormatByLocaleRef.current; | ||
|
||
let messageFormat: IntlMessageFormat; | ||
if (cachedFormatByLocale[locale]?.[idPath] != null) { | ||
messageFormat = cachedFormatByLocale[locale][idPath]; | ||
} else { | ||
const message = resolvePath(intlMessage, idPath); | ||
|
||
if (typeof message == 'object') { | ||
throw new Error(); | ||
} | ||
|
||
messageFormat = new IntlMessageFormat(message, locale); | ||
if (!cachedFormatByLocale[locale]) { | ||
cachedFormatByLocale[locale] = {}; | ||
} | ||
cachedFormatByLocale[locale][idPath] = messageFormat; | ||
} | ||
|
||
return messageFormat.format(value); | ||
}; | ||
} |
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,4 @@ | ||
import { cleanup } from '@testing-library/react'; | ||
import { afterEach } from 'vitest'; | ||
|
||
afterEach(cleanup); |
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,41 @@ | ||
import { LitIntlProvider } from '../src/lit-intl.provider'; | ||
import { type IntlMessage } from '../src/types/intl-message'; | ||
import { type TranslationValue } from '../src/types/translation'; | ||
import { useTranslation } from '../src/use-translation'; | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import { it } from 'vitest'; | ||
|
||
const message: IntlMessage = { | ||
Basic: { | ||
Hello: 'Hello', | ||
'Hello {name}': 'Hello {name}', | ||
}, | ||
}; | ||
|
||
function Provider({ children }: React.PropsWithChildren) { | ||
return <LitIntlProvider message={message}>{children}</LitIntlProvider>; | ||
} | ||
|
||
function renderMessage(message: string, values?: TranslationValue) { | ||
function Component() { | ||
const t = useTranslation('Basic'); | ||
return <>{t(message, values)}</>; | ||
} | ||
|
||
return render( | ||
<Provider> | ||
<Component /> | ||
</Provider>, | ||
); | ||
} | ||
|
||
it('should be print Hello', () => { | ||
renderMessage('Hello'); | ||
screen.getByText('Hello'); | ||
}); | ||
|
||
it('should be print Hello with name value', () => { | ||
renderMessage('Hello {name}', { name: 'world' }); | ||
screen.getByText('Hello world'); | ||
}); |
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,5 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { environment: 'jsdom', setupFiles: './test/setup.ts' }, | ||
}); |
Oops, something went wrong.