Skip to content

[Translator] Add an option to throw an exception instead of returning the message as is when the translation is not found. #2183

Closed
@Crovitche-1623

Description

@Crovitche-1623

For some translations, the id (the key in other words) is the same as the translations (e.g. the Invalid credentials. in the security domain):

export const INVALID_CREDENTIALS = {"id":"Invalid credentials.","translations":{"security":{"fr":"Identifiants invalides.","en":"Invalid credentials.","de":"Fehlerhafte Zugangsdaten.","it":"Credenziali non valide."}}};

Therefore, there is no way in the frontend to distinguish a missing translation from a translation that have the same id (key) same as a translation because the only way I found in the frontend to detect if the translation is missing is checking if the result from the trans method is the same as the id. For example:

const translationConstant = aServiceThat.findConstantById(messageId);

// I'm voluntarily trying in the default domain (`messages`) even though I know the message isn't there.
let translatedMessage = translator.trans(translator[translationConstant]);

if (translatedMessage === translator[translationConstant].id) {
    // Here how I distinguish a missing translation from a translation that have a same value as his id when no exception is thrown. If an exception was thrown, I could try something else (e.g. search in another domain)
}

To solve this problem, I think we could pass an additional to the trans method with a default value (throwWhenNotFound = false) and catch the exception later.

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