11import { describe , expect , test } from 'vitest' ;
22import { createTranslationSystemFromFs } from '../../utils/translations-fs' ;
3+ import { YAMLException } from 'js-yaml' ;
34
45describe ( 'createTranslationSystemFromFs' , ( ) => {
56 test ( 'creates a translation system that returns default strings' , ( ) => {
@@ -18,14 +19,21 @@ describe('createTranslationSystemFromFs', () => {
1819 test ( 'creates a translation system that uses custom strings' , ( ) => {
1920 const useTranslations = createTranslationSystemFromFs (
2021 {
21- locales : { en : { label : 'English' , dir : 'ltr' } } ,
22+ locales : {
23+ en : { label : 'English' , dir : 'ltr' , lang : 'en' } ,
24+ fr : { label : 'Français' , dir : 'ltr' , lang : 'fr' } ,
25+ } ,
2226 defaultLocale : { label : 'English' , locale : 'en' , dir : 'ltr' } ,
2327 } ,
2428 // Using `src/` to load custom files in this test fixture.
2529 { srcDir : new URL ( './src/' , import . meta. url ) }
2630 ) ;
27- const t = useTranslations ( 'en' ) ;
31+ // From an i18n JSON file
32+ let t = useTranslations ( 'en' ) ;
2833 expect ( t ( 'page.editLink' ) ) . toMatchInlineSnapshot ( '"Make this page different"' ) ;
34+ // From an i18n YAML file
35+ t = useTranslations ( 'fr' ) ;
36+ expect ( t ( 'page.editLink' ) ) . toMatchInlineSnapshot ( '"Rendre cette page différente"' ) ;
2937 } ) ;
3038
3139 test ( 'supports root locale' , ( ) => {
@@ -68,12 +76,22 @@ describe('createTranslationSystemFromFs', () => {
6876 expect ( ( ) =>
6977 createTranslationSystemFromFs (
7078 { locales : { } , defaultLocale : { label : 'English' , locale : 'en' , dir : 'ltr' } } ,
71- // Using `malformed-src/` to trigger syntax error in bad JSON file.
72- { srcDir : new URL ( './malformed-src/' , import . meta. url ) }
79+ // Using `malformed-json- src/` to trigger syntax error in bad JSON file.
80+ { srcDir : new URL ( './malformed-json- src/' , import . meta. url ) }
7381 )
7482 ) . toThrow ( SyntaxError ) ;
7583 } ) ;
7684
85+ test ( 'throws on malformed i18n YAML' , ( ) => {
86+ expect ( ( ) =>
87+ createTranslationSystemFromFs (
88+ { locales : { } , defaultLocale : { label : 'English' , locale : 'en' , dir : 'ltr' } } ,
89+ // Using `malformed-yaml-src/` to trigger syntax error in bad YAML file.
90+ { srcDir : new URL ( './malformed-yaml-src/' , import . meta. url ) }
91+ )
92+ ) . toThrow ( YAMLException ) ;
93+ } ) ;
94+
7795 test ( 'creates a translation system that uses custom strings injected by plugins' , ( ) => {
7896 const useTranslations = createTranslationSystemFromFs (
7997 {
0 commit comments