@@ -88,6 +88,7 @@ let validLocales: Set<string> | undefined;
8888let loadLocale : ( ( locale : string ) => Promise < LocaleModule > ) | undefined ;
8989let templates : TemplateMap | undefined ;
9090let loading = new Deferred < void > ( ) ;
91+ const changeLocaleCallbacks = new Set < ( ) => void > ( ) ;
9192
9293/**
9394 * Set runtime configuration parameters for lit-localize. This function must be
@@ -134,12 +135,14 @@ export const setLocale: ((newLocale: string) => void) & {
134135 }
135136 if ( newLocale === sourceLocale ) {
136137 loading . resolve ( ) ;
138+ fireChangeLocaleCallbacks ( ) ;
137139 } else {
138140 loadLocale ( newLocale ) . then (
139141 ( mod ) => {
140142 if ( newLocale === activeLocale ) {
141143 templates = mod . templates ;
142144 loading . resolve ( ) ;
145+ fireChangeLocaleCallbacks ( ) ;
143146 }
144147 // Else another locale was requested in the meantime. Don't resolve or
145148 // reject, because the newer load call is going to use the same promise.
@@ -163,6 +166,40 @@ export const localeReady: (() => Promise<void>) & {
163166 _LIT_LOCALIZE_LOCALE_READY_ ?: never ;
164167} = ( ) => loading . promise ;
165168
169+ /**
170+ * Add the given function to the set of callbacks that will be invoked whenever
171+ * the locale changes and its localized messages are ready.
172+ *
173+ * Use this function to re-render your application whenever the locale is changed.
174+ *
175+ * If you are using LitElement, consider using LocalizedLitElement, which performs
176+ * this re-rendering automatically.
177+ */
178+ export const addLocaleChangeCallback : ( ( callback : ( ) => void ) => void ) & {
179+ _LIT_LOCALIZE_ADD_LOCALE_CHANGE_CALLBACK_ ?: never ;
180+ } = ( callback : ( ) => void ) => {
181+ changeLocaleCallbacks . add ( callback ) ;
182+ } ;
183+
184+ /**
185+ * Remove the given function from the set of callbacks that will be invoked
186+ * whenever the locale changes and its localized messages are ready.
187+ */
188+ export const removeLocaleChangeCallback : ( ( callback : ( ) => void ) => void ) & {
189+ _LIT_LOCALIZE_REMOVE_LOCALE_CHANGE_CALLBACK_ ?: never ;
190+ } = ( callback : ( ) => void ) => {
191+ changeLocaleCallbacks . delete ( callback ) ;
192+ } ;
193+
194+ /**
195+ * Fire all of the registered change locale callback functions.
196+ */
197+ const fireChangeLocaleCallbacks = ( ) => {
198+ for ( const callback of changeLocaleCallbacks ) {
199+ callback ( ) ;
200+ }
201+ } ;
202+
166203/**
167204 * Make a string or lit-html template localizable.
168205 *
0 commit comments