From d798aa2cdfdd7c321872c3a0208fc118a259edb2 Mon Sep 17 00:00:00 2001 From: vjrj Date: Thu, 1 Feb 2018 06:12:01 +0100 Subject: [PATCH] i18n setting right --- imports/api/Users/server/publications.js | 12 +++++++++++- imports/startup/client/i18n.js | 8 ++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/imports/api/Users/server/publications.js b/imports/api/Users/server/publications.js index 913f41f..08787d6 100644 --- a/imports/api/Users/server/publications.js +++ b/imports/api/Users/server/publications.js @@ -8,7 +8,17 @@ Meteor.publish('users.editProfile', function usersProfile() { fields: { emails: 1, profile: 1, - services: 1 + services: 1, + lang: 1 } }); }); + +Meteor.publish('userData', function userData() { + if (this.userId) { + return Meteor.users.find({ _id: this.userId }, { + fields: { lang: 1 } + }); + } + this.ready(); +}); diff --git a/imports/startup/client/i18n.js b/imports/startup/client/i18n.js index 7e1a6e3..2a3398d 100644 --- a/imports/startup/client/i18n.js +++ b/imports/startup/client/i18n.js @@ -100,22 +100,26 @@ i18n.use(backend) CookieConsent.init(cookiesOpt); }); +Meteor.subscribe('userData'); // lang is there + i18n.on('languageChanged', (lng) => { moment.locale(lng); T9n.setLanguage(lng); - Tracker.autorun(() => { + Tracker.autorun((computation) => { if (Meteor.userId()) { // logged if (Meteor.user() && (typeof Meteor.user().lang === 'undefined' || Meteor.user().lang !== lng) ) { // Set the autodetected/changed lang - console.log(`Setting the autodetected lang ${lng}`); + console.log(`Setting the autodetected lang ${lng}, from previous ${Meteor.user().lang}`); + // console.log(Meteor.user()); Meteor.call('users.setLang', lng, (error) => { if (error) { console.error(error); } }); + computation.stop(); // not need it again til new lang change } } });