Skip to content

Feature/localization #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion __tests__/suits/Validator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,23 @@ describe('test validator', () => {
await validator.validate();

expect(validator.getErrors()[0]).toBe('Value is false negative required');
})
});

it('should change locale if it exists', () => {
const messages = {
test: {
foo: 'bar'
}
};

Validator.setLocale('test', messages);

expect(Validator.getLocale()).toBe('test');
});

it('should default to en if changing to an non-existing locale', () => {
Validator.setLocale('not_existing');

expect(Validator.getLocale()).toBe('en');
});
});
3 changes: 3 additions & 0 deletions src/LocaleMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type LocaleMessages = Record<string, string>;

export type LocaleMessagesMap = Record<string, LocaleMessages>;
57 changes: 54 additions & 3 deletions src/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
createIntlCache,
IntlCache,
IntlShape
} from '@formatjs/intl'
} from '@formatjs/intl';
import { Rule, RuleFunction, RuleObject } from './Rule';
import { RuleOptions } from './RuleOptions';
import { capitalize } from './common/utils';
import { ValidatorArea } from './components/ValidatorArea';
import required from './rules/required';
import { getValue, isCanvasElement } from './common/dom';
import { LocaleMessagesMap } from './LocaleMessages';
import { capitalize } from './common/utils';
import en from './locale/en';

export class Validator {
public static VALIDATABLE_ELEMENTS: string[] = [
Expand Down Expand Up @@ -63,6 +65,16 @@ export class Validator {
*/
private validationName?: string;

/**
* The active locale
*/
private static locale = 'en';

/**
* Map with messages keyed with locales, containing EN as default
*/
private static messages: LocaleMessagesMap = en;

public constructor(
elements: HTMLElement[],
rules: RuleOptions,
Expand All @@ -83,7 +95,8 @@ export class Validator {
*/
private createIntl(): IntlShape<string> {
return createIntl({
locale: 'en'
locale: Validator.locale,
messages: Validator.messages[Validator.locale]
}, this.intlCache);
}

Expand Down Expand Up @@ -252,4 +265,42 @@ export class Validator {
public static ruleExists(name: string): boolean {
return Object.prototype.hasOwnProperty.call(Validator.rules, name);
}

public static hasLocale(locale: string): boolean {
return !!Object.prototype.hasOwnProperty.call(Validator.messages, locale);
}

/**
* Adds a locale map to the messages map, keyed by locale
*/
public static addLocale(messages: LocaleMessagesMap): void {
Validator.messages = {
...Validator.messages,
...messages
};
}

/**
* Sets the given locale for all the new created validator instance or defaults to English if the locale does not
* exist
*/
public static setLocale(locale: string, messages?: LocaleMessagesMap): void {
if (!messages) {
if (Validator.hasLocale(locale)) {
Validator.locale = locale;
} else {
Validator.locale = 'en';
}
} else {
Validator.addLocale(messages);
Validator.setLocale(locale);
}
}

/**
* Gets the current locale of the validator
*/
public static getLocale(): string {
return Validator.locale;
}
}
14 changes: 14 additions & 0 deletions src/locale/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LocaleMessagesMap } from '../LocaleMessages';

const en: LocaleMessagesMap = {
en: {
activeUrl: '{name} is not an active url',
checked: '{name} is not checked',
max: '{name} should be not greater than {0}',
min: '{name} should be at least {0}',
regex: '{name} doesn\'t have a valid format',
required: '{name} is required'
}
}

export default en;
15 changes: 15 additions & 0 deletions src/locale/nl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LocaleMessagesMap } from '../LocaleMessages';

const nl: LocaleMessagesMap = {
nl: {
activeUrl: '{} is geen actieve URL',
checked: '{name} is niet aangevinkt.',
max: '{name} max niet meer zijn dan {0}',
min: '{name} moet minstens {0} zijn',
regex: '{name} heeft geen geldig formaat',
required: '{name} is verplicht'
}
}

export default nl;

2 changes: 1 addition & 1 deletion src/rules/activeUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export default {
.length;
},
message(): string {
return 'Not an active url'
return 'activeUrl';
}
}
2 changes: 1 addition & 1 deletion src/rules/checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export default {
})
},
message(): string {
return '{name} is not checked.';
return 'checked';
}
};
2 changes: 1 addition & 1 deletion src/rules/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default {
})
},
message(): string {
return `{name} should be not greater than {0}`
return 'max';
}
};
2 changes: 1 addition & 1 deletion src/rules/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default {
})
},
message(): string {
return `{name} should be at least {0}`
return 'min';
}
};
2 changes: 1 addition & 1 deletion src/rules/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export default {
})
},
message(): string {
return '{name} heeft een ongeldig formaat';
return 'regex';
}
}
2 changes: 1 addition & 1 deletion src/rules/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ export default {
},

message(): string {
return `{name} is required`;
return 'required';
}
};