Multi language pages (access @session from a model)


#1

Hi,

I’m creating a simple Ruby on Rails based CMS. It will support multiple
languages. Therefore I’ve a table ‘pages’ and a table
‘page_translations’. The table ‘page’ contains a title and body in the
default language, the table ‘page_translations’ contain a title and body
for an other language.

Now I would like to have an easy way to retrieve the title and body of a
page given the current language (stored in the session). I would like to
put this logic in a model (the Page model, or maybe a new model like
‘TranslatedPage’). This way I don’t have to put ugly code in my
controllers and views everytime to fetch the page for the current
language.

However, it seems I can’t access @session from the model. Does anybody
have a good idea on how to implement this?

Thanks in advance!

Greetings,

  • Matthias

#2

On 24 Dec 2005, at 18:47, Matthias Pronk wrote:

However, it seems I can’t access @session from the model. Does anybody
have a good idea on how to implement this?

Try dropping the @ – use session instead of @session. Any good?

Yours,
Craig

Craig W. | t: +44 (0)131 516 8595 | e: removed_email_address@domain.invalid
Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net


#3

Matthias Pronk a écrit :

controllers and views everytime to fetch the page for the current
language.

However, it seems I can’t access @session from the model. Does anybody
have a good idea on how to implement this?

My 0.02$: I think you should normalize your tables, and have

DEFAULT_LANG= ‘en’

page_translations
id
lang CHAR(2) DEFAULT en
mycontent TEXT

then in your controller you simply do

MyController.show
@my_model = MyModel.find(12, :include => :page_translations,
:conditions => [‘lang = ?’, session[:lang]])
end

Why is this ugly ?