microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.48k stars 28.97k forks source link

Add tagged template literal support for `localize` calls #183566

Open TylerLeonhardt opened 1 year ago

TylerLeonhardt commented 1 year ago

Similar to https://github.com/microsoft/vscode/issues/181555 but for core strings.

We propose adding an overload to localize that allows it to be used with tagged template literals. With tagged literals, developers use a template literal as they "normally would", but prepend it with the method call

localize`Hello ${name}!`

The associated definition for localize() would gain an overload:

    export namespace l10n {
        /**
         * Marks a string for localization. If a localized bundle is available for the language specified by
         * {@link env.language} and the bundle has a localized value for this message, then that localized
         * value will be returned (with injected {@link args} values for any templated values).
         *
         * This signature is for use with tagged template literals. The more verbose object-based
         * call should be used if comments are required.
         *
         * @example
         * ```
         * localize`Hello ${name}!`
         * ```
         *
         * @param message - String message components.
         * @param args - Replacement components in the string.
         * @returns localized string with injected arguments.
         * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates
         */
        export function t(parts: TemplateStringsArray, ...args: Array<string | number | boolean>): string;
TylerLeonhardt commented 1 year ago

I think maybe we want to do this work after the move to ES modules as localization might change drastically then.