Skip to content
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

Added translation support #18

Merged
merged 8 commits into from Jun 3, 2017
Merged

Added translation support #18

merged 8 commits into from Jun 3, 2017

Conversation

DJWassink
Copy link
Collaborator

@DJWassink DJWassink commented Apr 24, 2017

Hey guys a few days ago I added support for translation and have been running it locally here for a while and it works quite okay.

The api is pretty simple and I will give a example with the email validator.
As you can see on line 56 of validations.js master...change/localization#diff-6fb54c87831f7e0eeab9cbbbc5b2b9feR56
the code looks like this:

translator(customMessage || 'email', {paramName: paramName}, locals)

the translator takes a message, which is either a customMessage or a name pointing to a translation. If this name (in this case 'email') is present in the translation JSON its will replace the customMessage.

Without a customMessage it will take the string on: master...change/localization#diff-f06d6e49f5230c6ec1d195c064770b81R3
And the translator will try to fill the message with properties of the second argument of the translator function in the translation string, so in the email string : 'The ":paramName" must be a valid email address',
:paramName will be replace with the paramName variable. (see the second argument in the aboven snippet {paramName: paramName})

For users the default error string is already quite usable but it they want they are able to supply there own customMessage and choose themself if they use the custom properties or not.

@nettofarah
Copy link
Owner

Hey, @DJWassink.
Thanks for the PR.

This is a bit of big change.
How we have to look into some other time when I have chance.

@DJWassink
Copy link
Collaborator Author

Hey @nettofarah just touching back on this, did you have a change to look at it yet? 😃

@nettofarah
Copy link
Owner

Hey, @DJWassink.
Sorry, I've been super busy.

I'll be able to check it out this weekend though

@@ -0,0 +1,32 @@
var local = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to check the indentation here.
I'm sticking to 2 spaces.

@@ -0,0 +1,22 @@
var get = require('lodash/get');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this to 2 spaces

var fallbackTransMessage = get(fallbackLocals, message, message);
var transMessage = get(localization, message, fallbackTransMessage);

Object.keys(params).forEach(function(key) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to support ES5 with this codebase.
So we can probably swap Object.keys with some lodash helper function

Copy link
Collaborator Author

@DJWassink DJWassink May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the funny thing, Object.keys is actually ES5 ;). Just to be sure I will swap it out with forOwn.

Maybe also better to use lodash for this, even though Object.keys is ES5 I would't be surprised if it also iterates the prototype's

if (typeof localization === 'undefined') {
localization = fallbackLocals;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's rename trans to translated for clarity

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could your clarify this one a bit? So far as I can see there isnt a trans variable? Or do you wanna change translator to translated?

var fallbackLocals = require('./translations/en');

function translate(message, params, localization) {
if (typeof localization === 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

@nettofarah nettofarah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good.
Except for some style changes I requested.

Let's also keep the old english locales and revisit them in an old PR. (I would want this change to be a minor change and not introduce breaking changes just yet).

Also, can you update the README to include documentation about translations?

@nettofarah
Copy link
Owner

nettofarah commented May 26, 2017 via email

@DJWassink
Copy link
Collaborator Author

DJWassink commented May 28, 2017

Hey @nettofarah just checking back on what you meant with "let's rename trans to translated for clarity". I think this is the only issue remaining for a merge 🎉 🌮

@nettofarah
Copy link
Owner

@DJWassink: I meant that the name of the variable is a little weird.
Let's just rename every occurrence of trans to translated

localization = fallbackLocals;
}

var fallbackTransMessage = get(fallbackLocals, message, message);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallbackTranslationMessage

}

var fallbackTransMessage = get(fallbackLocals, message, message);
var transMessage = get(localization, message, fallbackTransMessage);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translatedMessage


forOwn(params, function (value, key) {
var regex = new RegExp(escapeRegExp(':' + key), 'g');
transMessage = replace(transMessage, regex, params[key]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translationMessage

@nettofarah
Copy link
Owner

nettofarah commented Jun 2, 2017

@DJWassink: just left a few last comments.
Feel free to squash & merge after that :)

@DJWassink DJWassink merged commit f3baed5 into master Jun 3, 2017
@DJWassink DJWassink deleted the change/localization branch June 3, 2017 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants