@lit/localize
Advanced tools
Comparing version 0.10.2 to 0.10.3
@@ -22,5 +22,7 @@ /** | ||
/** | ||
* Required function that returns a promise of the localized templates for the | ||
* given locale code. For security, this function will only ever be called | ||
* with a `locale` that is contained by `targetLocales`. | ||
* Required function that returns the localized templates for the given locale | ||
* code. | ||
* | ||
* This function will only ever be called with a `locale` that is contained by | ||
* `targetLocales`. | ||
*/ | ||
@@ -27,0 +29,0 @@ loadLocale: (locale: string) => Promise<LocaleModule>; |
@@ -7,7 +7,5 @@ /** | ||
import { _installMsgImplementation } from '../lit-localize.js'; | ||
import { generateMsgId } from '../internal/id-generation.js'; | ||
import { Deferred } from '../internal/deferred.js'; | ||
import { LOCALE_STATUS_EVENT } from '../internal/locale-status-event.js'; | ||
import { joinStringsAndValues } from '../internal/str-tag.js'; | ||
import { defaultMsg } from '../internal/default-msg.js'; | ||
import { runtimeMsg } from '../internal/runtime-msg.js'; | ||
/** | ||
@@ -40,3 +38,3 @@ * Dispatch a "lit-localize-status" event to `window` with the given detail. | ||
export const configureLocalization = (config) => { | ||
_installMsgImplementation(runtimeMsg); | ||
_installMsgImplementation(((template, options) => runtimeMsg(templates, template, options))); | ||
activeLocale = sourceLocale = config.sourceLocale; | ||
@@ -115,55 +113,2 @@ validLocales = new Set(config.targetLocales); | ||
}; | ||
function runtimeMsg(template, options) { | ||
var _a; | ||
if (templates) { | ||
const id = (_a = options === null || options === void 0 ? void 0 : options.id) !== null && _a !== void 0 ? _a : generateId(template); | ||
const localized = templates[id]; | ||
if (localized) { | ||
if (typeof localized === 'string') { | ||
// E.g. "Hello World!" | ||
return localized; | ||
} | ||
else if ('strTag' in localized) { | ||
// E.g. str`Hello ${name}!` | ||
// | ||
// Localized templates have ${number} in place of real template | ||
// expressions. They can't have real template values, because the | ||
// variable scope would be wrong. The number tells us the index of the | ||
// source value to substitute in its place, because expressions can be | ||
// moved to a different position during translation. | ||
return joinStringsAndValues(localized.strings, | ||
// Cast `template` because its type wasn't automatically narrowed (but | ||
// we know it must be the same type as `localized`). | ||
template.values, localized.values); | ||
} | ||
else { | ||
// E.g. html`Hello <b>${name}</b>!` | ||
// | ||
// We have to keep our own mapping of expression ordering because we do | ||
// an in-place update of `values`, and otherwise we'd lose ordering for | ||
// subsequent renders. | ||
let order = expressionOrders.get(localized); | ||
if (order === undefined) { | ||
order = localized.values; | ||
expressionOrders.set(localized, order); | ||
} | ||
// Cast `localized.values` because it's readonly. | ||
localized.values = order.map((i) => template.values[i]); | ||
return localized; | ||
} | ||
} | ||
} | ||
return defaultMsg(template); | ||
} | ||
const expressionOrders = new WeakMap(); | ||
const hashCache = new Map(); | ||
function generateId(template) { | ||
const strings = typeof template === 'string' ? template : template.strings; | ||
let id = hashCache.get(strings); | ||
if (id === undefined) { | ||
id = generateMsgId(strings, typeof template !== 'string' && !('strTag' in template)); | ||
hashCache.set(strings, id); | ||
} | ||
return id; | ||
} | ||
//# sourceMappingURL=runtime.js.map |
{ | ||
"name": "@lit/localize", | ||
"version": "0.10.2", | ||
"version": "0.10.3", | ||
"publishConfig": { | ||
@@ -25,6 +25,2 @@ "access": "public" | ||
"files": [ | ||
"/src/", | ||
"!/src/tests/", | ||
"/src_client/", | ||
"!/src_client/tests/", | ||
"/lit-localize.d.ts", | ||
@@ -31,0 +27,0 @@ "/lit-localize.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
87259
46
1025