All files / packages/configs/src/playwright/utils i18n.ts

100% Statements 12/12
100% Branches 8/8
100% Functions 7/7
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                      1x 2x         1x           2x 4x       1x           1x 4x   8x 4x     4x 4x      
import { createI18n } from "vue-i18n";
 
type Messages = Record<string, unknown>;
 
// vue-i18n types messages by schema, and a locale file read as JSON has none
type AnyMessages = NonNullable<Parameters<typeof createI18n>[0]["messages"]>;
 
/**
 * A `t` over one locale's messages, worded by vue-i18n itself, that throws on a key the messages
 * lack instead of printing the key, so a spec naming a control by its key fails when the key goes.
 */
export const createT = (messages: Messages, locale = "es-ES") => {
    const i18n = createI18n({
        legacy: false,
        locale,
        messages: { [locale]: messages } as AnyMessages,
        missing: (_, key) => {
            throw new Error(`No message for "${key}" in ${locale}`);
        },
        missingWarn: false,
        fallbackWarn: false,
    });
 
    return (key: string, named: Record<string, unknown> = {}, plural?: number): string =>
        plural === undefined ? i18n.global.t(key, named) : i18n.global.t(key, named, plural);
};
 
// A host such as `www.revista.es` or an address's domain is never a key, so a path can't follow one
const DOTTED_PATH = /(?<![\w.@/-])[A-Z_][\w-]*(?:\.[\w-]+)+/gi;
 
/**
 * The dotted paths printed in `text` that start with one of the messages' namespaces, which is
 * what vue-i18n renders for a key it lacks, a key built from a variable included.
 */
export const rawKeysIn = (text: string, messages: Messages): string[] => {
    const namespaces = new Set(
        Object.entries(messages)
            .filter(([, value]) => value !== null && typeof value === "object")
            .map(([key]) => key),
    );
 
    return [...new Set(text.match(DOTTED_PATH) ?? [])].filter((path) =>
        namespaces.has(path.split(".")[0]!),
    );
};