globalize2: querying translated fields

9 views
Skip to first unread message

hydrozen

unread,
Nov 30, 2008, 4:18:54 PM11/30/08
to rails-i18n
How exactly can I query fields that are translated using Globalize2?
Can I still use find_by_ methods?
Let's say i want to find the Post for which the english title is
"foo" ? How would I do that?

Sven Fuchs

unread,
Dec 7, 2008, 4:11:36 PM12/7/08
to rails...@googlegroups.com
I'd need to verify this myself, but Globalize2 uses ActiveRecord's own
features to implement model translations, so you can always just use
those default features to lookup whatever is interesting to you.

Offhand:

Post.find :first, :include => :post_translations, :conditions =>
{:title => 'foo', :locale => 'en'}

Sam Pohlenz

unread,
Dec 8, 2008, 5:15:29 PM12/8/08
to rails-i18n
Just tried this out to satisfy my curiosity and the required syntax
is:

Post.find(:first, :joins => :globalize_translations, :conditions =>
{ :post_translations => { :title => 'foo' } })

-Sam

Trond Lossius

unread,
Dec 26, 2008, 8:07:20 PM12/26/08
to rails...@googlegroups.com
I am struggling with a somewhat similar problem: I want to list all
entries in Pages. The fields :title and :body are translated using
Globalize2. I have most but not all of the pages translated into two
locales.


I'm able to get a list of all Pages by means of Pages.find(:all), but
the list is not sorted. Pages translated into current locale will show
up in current locale, the rest will show up in fallback locale.

Now I'd like to have the same items listed, but sorted alphabetically
based on the title field.

If I do the following:

Pages.find(:all,
:joins => :globalize_translations,
:order => :title )

all pages show up. If they have not been translated into the currently
set locale they will still show up in the other language. However,
pages translated into both languages shows up twice; both times with
the correct locale. I guess this is because I am now effectively
listing all entries in the Page_translations database table.

If I add the following condition:

Pages.find(:all,
:joins => :globalize_translations,
:conditions => { :pages_translations => { :locale => I18n.locale }},
:order => :title )

each page will be listed once only. But the problem now is that pages
that are not translated into current locale do not show up at all
anymore.

Is there any way of listing all pages ordered by title, in the current
locally if possible, if not in the other locale?


Thanks,
Trond

namxam

unread,
Jan 20, 2009, 6:38:24 AM1/20/09
to rails-i18n
I am having the exact same problem right now. Is there any answer /
hint how to solve this?

Max

Niels Ganser

unread,
Jan 21, 2009, 4:16:23 PM1/21/09
to rails-i18n
Hi guys.

I haven't spent a lot of thought on this, so the following is not
optimised in any way whatsoever, yet it will work and does no put any
additional load on the database. Beware though that when you have
translations in many different languages and/or your initial find
returns many objects, the code might run quite a while. Or not. Don't
know.. I guess the proof of the pudding is in the eating ;)

pages = Page.find(:all, :joins => :globalize_translations, :order
=> :title, :select => "pages.*, page_translations.locale AS
ordered_locale")
page_locale_ids = {}
pages.each { |page| page_locale_ids.include?(page.ordered_locale) ?
page_locale_ids[page.ordered_locale] << page.id : page_locale_ids
[page.ordered_locale] = [page.id]
pages.delete_if { |page| page.ordered_locale != I18n.locale and
page_locale_ids[I18n.locale].include?(page.id) }

Take note that this might need to be further adapted when you are
missing the translation for your primary locale yet several fallback
results are returned by the initial find. It should provide a decent
starting point though..

Best,
Niels

Mirko

unread,
Feb 6, 2009, 10:26:51 AM2/6/09
to rails-i18n
Do you have to have has_many :post_translations, because this doesn't
work for me: "Association not found".

On Dec 8 2008, 11:15 pm, Sam Pohlenz <spohl...@gmail.com> wrote:
> Just tried this out to satisfy my curiosity and the required syntax
> is:
>
> Post.find(:first, :joins => :globalize_translations, :conditions =>
> { :post_translations => { :title => 'foo' } })
>
> -Sam
>
> On Dec 7, 1:11 pm, Sven Fuchs <svenfu...@artweb-design.de> wrote:
>
> > I'd need to verify this myself, butGlobalize2uses ActiveRecord's own  
Reply all
Reply to author
Forward
0 new messages