Just-validate icon indicating copy to clipboard operation
Just-validate copied to clipboard

Not possible to translate default rule messages

Open rhandrade opened this issue 2 years ago • 2 comments

Currently it is not possible to translate the standard messages of the rules and continue using the replace values without having to rewrite the entire error message.

The library should be able to provide a way for users to specify only the rule and the translations they want, and the value swap functionality still work.

For example, if I don't specify a message in the errorMessage parameter and define a dictionary and set the locale equal to that dictionary, the library should load the translated messages.

The messages.ts file could be changed so that it returns a constant with the standard messages in English in the dictionary format

export const defaultDictionary = [
    {
        key: Rules.Required,
        dict: {                
            'en': 'The field is required',
        },
    },
    {
        key: Rules.Email,
        dict: {                
            'en': 'Email has invalid format',
        },
    },        
    {
        key: Rules.MaxLength,
        dict: {                
            'en': 'The field must contain a maximum of :value characters',
        },
    },                
    {
        key: Rules.MinLength,
        dict: {                
            'en': 'The field must contain a minimum of :value characters',
        },
    },
    {
        key: Rules.Password,
        dict: {                
            'en': 'Password must contain minimum eight characters, at least one letter and one number',
        },
    },        
    {
        key: Rules.StrongPassword,
        dict: {                
            'en': 'Password should contain minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character',
        },
    },
    {
        key: Rules.Number,
        dict: {                
            'en': 'Value should be a number',
        },
    },        
    {
        key: Rules.MaxNumber,
        dict: {                
            'en': 'Number should be less or equal than :value',
        },
    },        
    {
        key: Rules.MinNumber,
        dict: {                
            'en': 'Number should be more or equal than :value',
        },
    },        
    {
        key: Rules.MinFilesCount,
        dict: {                
            'en': 'Files count should be more or equal than :value',
        },
    },        
    {
        key: Rules.MaxFilesCount,
        dict: {                
            'en': 'Files count should be less or equal than :value',
        },
    },        
    {
        key: Rules.Files,
        dict: {                
            'en': 'Uploaded files have one or several invalid properties (extension/size/type etc).',
        },
    },
];

With this, the getLocalisedString function could be changed to always search for and treat the localized string, regardless of whether it is the default text or not.

getLocalisedString(rule: Rules, ruleValue?: FieldRuleValueType, str?: string): string {    
   
    const search = str ?? rule;
    let localisedStr = this.dictLocale.find((item) => item.key === search)?.dict[this.currentLocale];
    
    if(!str && !localisedStr){
        switch(rule){        
            case Rules.MaxLength:
            case Rules.MinLength:
            case Rules.MaxNumber:
            case Rules.MinNumber:
            case Rules.MinFilesCount:
            case Rules.MaxFilesCount:
                localisedStr = localisedStr.replace(':value', String(ruleValue!));
        }       
    }

    return (localisedStr || str);
    
}

rhandrade avatar Jun 17 '22 21:06 rhandrade

Hi! Thanks, great suggestion, I'll look into this

horprogs avatar Jun 23 '22 17:06 horprogs

Any update on this? Currently running into the same issue.

svondervoort avatar Oct 03 '22 11:10 svondervoort

Thanks guys, finally I allocated some time to work on this, seems should be fine now

horprogs avatar Dec 08 '22 18:12 horprogs