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

Collection.i18nFind query with translated field #28

Open
imajus opened this issue Apr 3, 2016 · 2 comments
Open

Collection.i18nFind query with translated field #28

imajus opened this issue Apr 3, 2016 · 2 comments

Comments

@imajus
Copy link

imajus commented Apr 3, 2016

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?

@imajus imajus changed the title Collection.i18nFind querty containing translated field Collection.i18nFind query with translated field Apr 3, 2016
@sagannotcarl
Copy link

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

@imajus
Copy link
Author

imajus commented Nov 10, 2016

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);
});

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

No branches or pull requests

2 participants