-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mark current <T> as legacy, new <T> uses snippets
- Loading branch information
Showing
6 changed files
with
126 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<script> | ||
import { t } from '.'; | ||
import { run } from 'svelte/legacy'; | ||
/** | ||
* @type {string} | ||
*/ | ||
export let msg; | ||
/** | ||
* @type {string} | ||
*/ | ||
export let ctx = undefined; | ||
/** | ||
* @type {string} | ||
*/ | ||
export let cmt = undefined; | ||
let strings; | ||
run(() => { | ||
strings = $t({ | ||
message: msg, | ||
context: ctx, | ||
comment: cmt | ||
}).split('#'); | ||
if (strings.length > 6) { | ||
console.error('svelte-i18n-lingui:', '<T> component can only have a maximum of 5 slots.'); | ||
} | ||
// TODO: Other prop validations | ||
// | ||
// else if (strings.length < 2) { | ||
// console.error( | ||
// 'svelte-i18n-lingui:', | ||
// '<T> component must only be used for interpolation and contain at least one # sign for slots.' | ||
// ); | ||
// } else if (strings.length !== Object.keys($$slots).length + 1) { | ||
// console.error( | ||
// 'svelte-i18n-lingui:', | ||
// "The number of slots on the message and the one passed in to the component dosent't match" | ||
// ); | ||
// } else { | ||
// strings.slice(1).forEach((str, i) => { | ||
// if (i === 0 && $$slots.default) { | ||
// console.error('abc'); | ||
// } | ||
// }); | ||
// } | ||
}); | ||
</script> | ||
|
||
<!-- | ||
@component | ||
A translation component used to support interpolation. | ||
If there is no need to interleave elements or components inside a message, | ||
the `t` store should be used instead for simplicity and consistency. | ||
- Usage: | ||
```svelte | ||
<T msg="Click # for more information" ctx="if any" cmt="if any"> | ||
<a href="https://example.com">{$t`here`}`</a> | ||
</T> | ||
``` | ||
--> | ||
|
||
<!-- Put in the same line to prevent automatic whtiespace insertion --> | ||
{strings[0] ?? ''}<slot />{strings[1] ?? ''}<slot name="1" />{strings[2] ?? ''}<slot | ||
name="2" | ||
/>{strings[3] ?? ''}<slot name="3" />{strings[4] ?? ''}<slot name="4" />{strings[5] ?? ''} |
File renamed without changes.
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,30 @@ | ||
import { it, expect } from 'vitest'; | ||
import { render, screen, cleanup } from '@testing-library/svelte'; | ||
import { messageCatalog } from '../helpers/test.js'; | ||
|
||
import T from './T.svelte'; | ||
import { locale } from '.'; | ||
|
||
it('When there are no translation catalog, show the message as-is', () => { | ||
render(T, { msg: 'hello' }); | ||
expect(screen.getByText('hello')).toBeDefined(); | ||
}); | ||
|
||
it('When translation catalog is set, show the translated message', () => { | ||
locale.set('ja', messageCatalog); | ||
render(T, { msg: 'hello' }); | ||
expect(screen.getByText('こんにちは')).toBeDefined(); | ||
}); | ||
|
||
it('Respects context and ignores comment when set', () => { | ||
render(T, { msg: 'right', ctx: 'direction' }); | ||
expect(screen.getByText('右')).toBeDefined(); | ||
cleanup(); | ||
|
||
render(T, { msg: 'right', ctx: 'correct' }); | ||
expect(screen.getByText('正しい')).toBeDefined(); | ||
cleanup(); | ||
|
||
render(T, { msg: 'right', ctx: 'correct', cmt: 'Comment for translator' }); | ||
expect(screen.getByText('正しい')).toBeDefined(); | ||
}); |
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