@@ -74,6 +74,21 @@ describe('i18n', () => {
7474 } ) ;
7575
7676 describe ( '#getFormatted' , ( ) => {
77+ it ( 'returns the formatted string' , ( ) => {
78+ const UIStrings = { testMessage : 'happy test' } ;
79+ const str_ = i18n . createMessageInstanceIdFn ( __filename , UIStrings ) ;
80+ const formattedStr = i18n . getFormatted ( str_ ( UIStrings . testMessage ) , 'en' ) ;
81+ expect ( formattedStr ) . toEqual ( 'happy test' ) ;
82+ } ) ;
83+
84+
85+ it ( 'returns the formatted string with replacements' , ( ) => {
86+ const UIStrings = { testMessage : 'replacement test ({errorCode})' } ;
87+ const str_ = i18n . createMessageInstanceIdFn ( __filename , UIStrings ) ;
88+ const formattedStr = i18n . getFormatted ( str_ ( UIStrings . testMessage , { errorCode : 'BOO' } ) , 'en' ) ;
89+ expect ( formattedStr ) . toEqual ( 'replacement test (BOO)' ) ;
90+ } ) ;
91+
7792 it ( 'throws an error for invalid locales' , ( ) => {
7893 // Populate a string to try to localize to a bad locale.
7994 const UIStrings = { testMessage : 'testy test' } ;
@@ -98,6 +113,50 @@ describe('i18n', () => {
98113 } ) ;
99114 } ) ;
100115
116+ describe ( '#registerLocaleData' , ( ) => {
117+ it ( 'installs new locale strings' , ( ) => {
118+ const localeData = {
119+ 'lighthouse-core/test/lib/i18n/i18n-test.js | testString' : {
120+ 'message' : 'en-XZ cuerda!' ,
121+ } ,
122+ } ;
123+ i18n . registerLocaleData ( 'en-XZ' , localeData ) ;
124+
125+ const UIStrings = { testString : 'en-US string!' } ;
126+ const str_ = i18n . createMessageInstanceIdFn ( __filename , UIStrings ) ;
127+ const formattedStr = i18n . getFormatted ( str_ ( UIStrings . testString ) , 'en-XZ' ) ;
128+ expect ( formattedStr ) . toEqual ( 'en-XZ cuerda!' ) ;
129+ } ) ;
130+
131+ it ( 'overwrites existing locale strings' , ( ) => {
132+ const filename = 'lighthouse-core/audits/is-on-https.js' ;
133+ const UIStrings = require ( '../../../../lighthouse-core/audits/is-on-https.js' ) . UIStrings ;
134+ const str_ = i18n . createMessageInstanceIdFn ( filename , UIStrings ) ;
135+
136+ // To start with, we get back the intended string..
137+ const origTitle = i18n . getFormatted ( str_ ( UIStrings . title ) , 'es-419' ) ;
138+ expect ( origTitle ) . toEqual ( 'Usa HTTPS' ) ;
139+ const origFailureTitle = i18n . getFormatted ( str_ ( UIStrings . failureTitle ) , 'es-419' ) ;
140+ expect ( origFailureTitle ) . toEqual ( 'No usa HTTPS' ) ;
141+
142+ // Now we declare and register the new string...
143+ const localeData = {
144+ 'lighthouse-core/audits/is-on-https.js | title' : {
145+ 'message' : 'es-419 uses https!' ,
146+ } ,
147+ } ;
148+ i18n . registerLocaleData ( 'es-419' , localeData ) ;
149+
150+ // And confirm that's what is returned
151+ const newTitle = i18n . getFormatted ( str_ ( UIStrings . title ) , 'es-419' ) ;
152+ expect ( newTitle ) . toEqual ( 'es-419 uses https!' ) ;
153+
154+ // Meanwhile another string that wasn't set in registerLocaleData just falls back to english
155+ const newFailureTitle = i18n . getFormatted ( str_ ( UIStrings . failureTitle ) , 'es-419' ) ;
156+ expect ( newFailureTitle ) . toEqual ( 'Does not use HTTPS' ) ;
157+ } ) ;
158+ } ) ;
159+
101160 describe ( 'Message values are properly formatted' , ( ) => {
102161 // Message strings won't be in locale files, so will fall back to values given here.
103162 const UIStrings = {
0 commit comments