ttag-org / ttag

:orange_book: simple approach for javascript localization
https://ttag.js.org/
MIT License
337 stars 40 forks source link

fix for buildStr function #266

Closed stoyanovk closed 8 months ago

stoyanovk commented 8 months ago

In this PR I fix issue https://github.com/ttag-org/ttag/issues/262

import { addLocale, t, useLocale } from "ttag";

let blank: unknown;
const index: Record<string, unknown> = {};
const key = "demo";

addLocale("fr", {
    headers: {
        "content-type": "text/plain; charset=utf-8",
        "plural-forms": "nplurals = 2; plural = (n > 1);"
    },
    translations: {
        "": {
            "Demo: ${ blank } ${ process.version } ${ index[key] }": {
                msgid: "Demo: ${ blank } ${ process.version } ${ index[key] }",
                msgstr: ["Démo: ${ blank } ${ process.version } ${ index[key] }"]
            }
        }
    }
});

function demo() {
    console.log(t`Demo: ${blank} ${process.version} ${index[key]}`);
}
demo();
useLocale("fr");
demo();

Result before PR:

Demo:  v18.16.0 
Démo: undefined v18.16.0 undefined

Result current:

Demo: undefined v18.16.0 undefined
Démo: undefined v18.16.0 undefined
AlexMost commented 8 months ago

Thanks, your fix is available in ttag@1.7.27