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

Context implemenation and some test cases #116

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

sameercaresu
Copy link

This is the implementation of pgettext and npgettext mentioned in #95.

I have added some test cases for pgettext and npgettext. Other tests, that I have not modified, are passing the tests as well.

Please let me know if you have any comments.

@sameercaresu
Copy link
Author

I have added implementation of dgettext, dngettext, dpgettext and dnpgettext.
If domain is used it is ignored during extraction.

@ArmorDarks
Copy link

Wow, that's nice! Thanks!

@sameercaresu
Copy link
Author

Any chance this could go into next release? Thanks.

Copy link
Collaborator

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the hard work! The code just needs some clean up and after that I think we're good to go.

}

// Always add the funcName as last element in return array
// If the gettext function's name starts with "np" (i.e. npgettext or np_) and its 3 arguments are strings, we regard it as context
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there documentation around this for existing gettext toolchain? If yes, it would be great to reference that here. If not, I'd like to hear more about your reasoning :)

Copy link
Author

Choose a reason for hiding this comment

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

I used GNU gettext as reference. npgettext is mentioned here. node-gettext implements this. Should I reference this here?


return [arg, node.arguments[1], funcName];

// If the gettext function's name starts with "p" (i.e. pgettext or p_) and its 2 arguments are strings, we regard it as context
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's now a lot of repetition around argument type checks here so I think we should move them into their own function. Something like:

function areArgsString(args) {
    // magic
    return wand;
}

Copy link
Author

Choose a reason for hiding this comment

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

Something like

// check if the args (object or array of objects) are 
function areArgsString(args) {
  return args && [].concat(args).every(function (arg) {
    return arg && (isStringLiteral(arg) || isStrConcatExpr(arg))
  });
}

And we call it like
if (funcName.substr(0, 2) === "np" && areArgsString(node.arguments.slice(0, 3)))

@@ -143,7 +159,7 @@ function parse(sources, options) {

// Always use the default context for now
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment needs updating?

Copy link
Author

Choose a reason for hiding this comment

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

Ok. Should remove this as we are using context now.

return;

var funcName = args.pop();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this logic should be isolated into getTranslatable. It should only return a structure with the fields context and translatable.

Copy link
Author

Choose a reason for hiding this comment

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

getTranslatable will return [context, translatable]

if (comments.extracted)
comments.extracted = comments.extracted.split('\n').filter(dedupeNCoalesce).join('\n');
Object.keys(translations).forEach(function (msgctxt) {
Object.keys(translations[msgctxt]).forEach(function (msgid){
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about you extract the inner function definition to outside of the forEach iterations and then pass msgctxt as the "context" argument to the inner forEach iteration? This way you can avoid redefining the inner function for each context iteration.

Copy link
Author

Choose a reason for hiding this comment

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

Something like

  function extractComments(msgid) {
      if (!this[msgid].comments)
        return;
      if (this[msgid].comments.reference)
        this[msgid].comments.reference = this[msgid].comments.reference.split('\n').filter(dedupeNCoalesce).join('\n');
      if (this[msgid].comments.extracted)
        this[msgid].comments.extracted = this[msgid].comments.extracted.split('\n').filter(dedupeNCoalesce).join('\n');
    }
    Object.keys(translations).forEach(function (msgctxt) {
      Object.keys(translations[msgctxt]).forEach(extractComments, translations[msgctxt]);
    });

testObj.ngettext("I'm also gonna get translated!", "I'm the plural form!", 2);
testObj.pgettext("context1", "I am translated in context!");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this reflects the real-world context usage since I expect them to come from a variable usually. What do you think? (I know if they are coming from a variable it is impossible to extract them as we do here in a static analysis)

Copy link
Author

@sameercaresu sameercaresu Jun 21, 2017

Choose a reason for hiding this comment

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

That looks like a typeo. It was correct before
testObj.gettext("I'm gonna get translated, yay!")

@@ -157,7 +173,7 @@ function parse(sources, options) {
});
Copy link
Author

Choose a reason for hiding this comment

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

Should add other keywords to aliases

if (options.keyword) {
    Object.keys(options.keyword).forEach(function (index) {
      ['n', 'p', 'np', 'd', 'dn', 'dp', 'dnp'].forEach(function (keyword) {
        options.keyword.push(keyword + options.keyword[index]);
      });
    });
  }

@sameercaresu
Copy link
Author

Hi I have made the requested changes. Please let me know if there's more changes needed.

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

4 participants