gadicc / meteor-messageformat

MessageFormat i18n support for Meteor, with reactive templates
54 stars 22 forks source link

[v2] Error calling setLocale #130

Open tcastelli opened 9 years ago

tcastelli commented 9 years ago

I'm getting this error whenever i call setLocale. Translation works on some cases, but having strange behaviour if i disableEval on browser policy. Otherwise, things seem to work but getting a lot of template exceptions after setLocale on things that were working with v0

I20150522-01:45:56.935(2)? Exception while invoking method 'msgfmt:setLocale' Er ror: When the modifier option is true, validation object must have at least one operator I20150522-01:45:56.936(2)? at checkModifier (packages/aldeed:simple-schema/s imple-schema-validation.js:271:1) I20150522-01:45:56.936(2)? at doValidation1 (packages/aldeed:simple-schema/s imple-schema-validation.js:321:1) I20150522-01:45:56.936(2)? at doValidation (packages/aldeed:simple-schema/si mple-schema-context.js:9:1) I20150522-01:45:56.936(2)? at SimpleSchemaValidationContext.simpleSchemaVali dationContextValidate as validate I20150522-01:45:56.936(2)? at [object Object].doValidate (packages/aldeed:co llection2/collection2.js:317:1) I20150522-01:45:56.936(2)? at [object Object].Mongo.Collection.(anonymous fu nction) as update I20150522-01:45:56.937(2)? at [object Object].Meteor.methods.msgfmt:setLocal e (packages/msgfmt:core/lib/mfPkg/messageformat-server.js:272:1) I20150522-01:45:56.937(2)? at packages/check/match.js:109:1 I20150522-01:45:56.937(2)? at [object Object]._.extend.withValue (packages/m eteor/dynamics_nodejs.js:56:1) I20150522-01:45:56.937(2)? at Object.Match._failIfArgumentsAreNotAllChecked (packages/check/match.js:108:1)

gadicc commented 9 years ago

Hey, the error is coming from simple-schema though. I'm not sure what this error means. I'm not saying msgfmt isn't related, but I need to know more. See if maybe there's anything useful in https://www.google.co.za/search?q=When+the+modifier+option+is+true%2C+validation+object+must+have+at+least+one+operator&oq=When+the+modifier+option+is+true%2C+validation+object+must+have+at+least+one+operator&aqs=chrome..69i57j0.365j0j7&sourceid=chrome&es_sm=122&ie=UTF-8, and maybe think if you have any of your own code that uses msgfmt but maybe depended on the v0 API.

Wow, can't believe you had problems with disallowEval. Spent so much time getting it working. As it happens though, this week I completely changed how all that stuff works. Just published msgfmt:core@2.0.0-preview.12, you can see if that makes any difference. It should.

tcastelli commented 9 years ago

seems like the disallowEval part is working now! :) more info about the other problem below

tcastelli commented 9 years ago

ok, I've been looking at the source code of messageformat v2 and the problem is quite easy to find

this.connection.locale = locale;
        if (this.userId && msgfmt.storeUserLocale)
          Meteor.users.update(this.userId, { $set : { locale: locale } });

I have attached the a SimpleSchema and collection2 verification to the Meteor.users collection and it was failing because I didnt update my schema with a locale field. Since i was already saving my locale on the user.profile object, do you think you could let the developer specify where to store the locale field? (Otherwise i would suggest storing it on user.profile by default since that's something the user could change without any additional allow/deny rules)

By now I'm trying to disable saving locale on user with

msgfmt.init(defLang, { // Send translations for all languages or current language
    sendPolicy: "current",

    // Don't invalidate msgfmt.locale() until new language is fully loaded
    waitOnLoaded: true,
    // Automatically adjust <body dir="rtl"> according to the language used
    setBodyDir: true,

    // Save setLocale() in Meteor.user().locale, sync to multiple clients
    storeUserLocale: false,

    // Use client's localStorage to avoid reloading unchanged translations
    useLocalStorage: false, // unless sendCompiled: true,
    // Send translations to the client pre-compiled
    sendCompiled: true // unless browserPolicy disallowUnsafeEval is set);

});

but when i start the application storeUserLocal is true so not sure who is changing the value again

DSpeichert commented 9 years ago

To make it clear: is the cause of the issue that you use simple-schema for validation on Meteor.users collection?

I can see how using profile.locale would be better in that case but this would also allow the client to change it by regular methods (and not setLocale() function). This may require more reactivity, I'm not sure if at the current moment it would propagate properly to change the language on the DDP connection.

tcastelli commented 9 years ago

yes, thats what I'm saying. and regarding using profile, thats wxactly what i meant, in the end user should be able to change it even if its not directly using setlocale so by just subscribing to profile.locale with autorun for example you could know when to invoke the setlocale. anyway since this can be disabled i just set it to false two times at the beginning (it worls with that trick) and im using the profile and calling manually to setlocale ()

DSpeichert commented 8 years ago

@gadicc need your input here :)