TAPevents / tap-i18n-db

MIT License
51 stars 17 forks source link

Collection.i18nFind query with translated field #28

Open imajus opened 8 years ago

imajus commented 8 years ago

I'm trying to fetch some records using i18nFind using translated field in a query, like so:

TAPi18n.publish('News.byTitle', function(title) {
    check(title, String);
    return News.i18nFind({ title: title });
})

That doesn't work when I choose non-English language in a client. I know there's matched record in database and can fetch it using News.find({ 'i18n.ru.title': title }).

Does i18nFind support this anyway? Maybe I'm just doing something wrong?

sagannotcarl commented 7 years ago

Did you solve this @imajus? What is the code you are using to subscribe to that publication?

imajus commented 7 years ago

Personally I've changed the logic so I don't need to search by translated field on the server anymore. Here's what I have:

// server
TAPi18n.publish('Entries.all', function() {
    return Entries.i18nFind({});
});
// client
TAPi18n.subscribe('Entries.all');
function getEntities() {
  const lang = TAPi18n.getLanguage();
  const sort = lang == 'en' ? { name: 1 } : { [`i18n.${lang}.name`]: 1 };
  return Entries.find({}, { sort });
}

As you can see I don't use translated field in search query anymore but for sorting option on client only. I bet if you find out how to get current language inside TAPi18n.publish you'll be able to use the same for your search query.

// server
TAPi18n.publish('Entries.byTitle', function(title) {
  const lang = 'en'; //FIXME: maybe this.lang? the latest function argument? something else? must be available somehow
  const query = lang == 'en' ? { title } : { [`i18n.${lang}.title`]: title };
  return Entries.i18nFind(query);
});