Thanks to visit codestin.com
Credit goes to github.com

Skip to content

InterpolatableTranslation should be a subtype of StrictTranslation #1605

@fls95

Description

@fls95

Current behavior

Applications may provide translations that are already compiled, like:

const myEnTranslations = {
   hello: () => "hello"
}

// assuming this runs in an injection context
function setTranslations() {
   const translateService = inject(TranslateService);

   translateService.setTranslation("en", myEnTranslations);
}

This used to work on the previous version (v16), but it started breaking on the latest one (v17).

Expected behavior

It should be possible to pass compiled translations like before without any further adjustments.

How do you think that we should fix this?

This was caused by a refactoring in the type system. Nothing has changed at runtime, and the error can be potentially ignored with @ts-ignore or @ts-expect-error.

To fix this, in my eyes StrictTranslation should be extended to include InterpolatableTranslation:

https://github.com/ngx-translate/core/blob/develop/projects/ngx-translate/src/lib/translate.service.interface.ts#L6-L14

Notice that this behavior is actually expected by default, in the sense that the default compiler is no-op:

https://github.com/ngx-translate/core/blob/develop/projects/ngx-translate/src/lib/translate.compiler.ts#L22

As the comment suggests, in case there is no need for a compiler at all, i.e., in case someone has already compiled the translations beforehand.

Minimal reproduction of the problem with instructions

// 1. Create an object with string translation keys and functions as values.
const demoEnTranslations = {
   "hello": () => "hello",
   "hello-params": (params) => "hello" + params.world
}

// 2. Call `setTranslation` with the translations object defined above.
function setTranslations() {
   const translateService = inject(TranslateService);

   translateService.setTranslation("en", myEnTranslations);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Argument of type [...] not assignable to 'TranslationObject' 
}

Environment


ngx-translate version: 17.0.0
Angular version: 21.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions