Topic: Bilingual website

Hi,
I want to create a large bilingual website (English/German), but wonder how to approach this project. Each piece of content needs to be available in both languages, and a lot of it will be pulled from a database. There will also be a button to switch from one language to the other. Has anybody ever done a similar project or can hint me into some direction?
Any idea is appreciated!

Re: Bilingual website

I would check for a translater gem out there or a plug-in. I'm sure there's something at rubyforge.

The hard way (which would just take time) is to save everything in a database with a row for english, row for spanish, etc. and have the display change that way.

I would check for a translater gem though.

-matthew

matthewvb

Re: Bilingual website

Hm, from my experience I can tell you than automatic translation almost never works. Human languages are not like computer language, where you can run a script to convert from one language into the other. In human languages, it always takes a human to do the translation work. Else too many of the subtle meanings get lost, and too many grammar errors will occur.
I think I should better go for a database with different rows, one for English, one for German, and so on... I just wonder if anybody here has ever done something similar before and could hint me into the right direction...

Re: Bilingual website

Another idea is to use partials. you can make english and german folders in your views folder and place the same partials in each, but in the appropriate language. You can then create a helper method to render the right partial based on the user's preference.

Railscasts - Free Ruby on Rails Screencasts

Re: Bilingual website

After thinking about it a bit, a database does feel like a better solution. This allows you to create a web interface for the translators so they don't have to have access to the files. I think this also offers more flexibility.

What I would do is create two tables: languages and translations. Of course you can rename to your liking. The languages table holds a row for each language along with the name. The translations table holds a foreign key linking to the language, a "key" identifier which is a string to reference the content easily, and lastly, the main content as a text block.

create_table :languages do |t|
  t.column :name, :string
end

create_table :translations do |t|
  t.column :language_id, :integer
  t.column :key, :string
  t.column :content, :text
end


You could store the id of the preferred language in the user's session, then create a helper method to fetch the appropriate content. For example:

# in view
<%= translation('home_introduction') %>

# in application_helper.rb
def translation(key)
  Translation.find_by_key_and_language_id(key, session[:language_id]).content
end


Of course you'll need to add error handling and defaults, but I think it's a good way to go.

Note: "key" might be a reserved word for a column so I don't know if it will work well.

Railscasts - Free Ruby on Rails Screencasts

Re: Bilingual website

Yes, I definitely need a database because the contents will have to remain editable. What I have in mind is a content management system with a split screen: The left half of the user interface will contain the English content, and the right half the German content. This way, the CMS will be ideal for bilingual people (like me) because they can create text in language A on the left, and then immediately afterwards add the text for language B, without having to constantly refer back and forth between the pages.

I don't actually understand why you suggest to use two database tables. Wouldn't just one be enough? What I have in mind is this:

create table contents (
    id            int            not null auto_increment,
    keyword        varchar(100)    not null,
    english        text            not null,
    german        text            not null,
    primary key (id)   
    );

To initially populate the table with some data, one might run a script like this:
insert into contents values(null, 'page1_title', 'Welcome', 'Willkommen');
insert into contents values(' ', 'page2_title', 'Products', 'Produkte');
insert into contents values(' ', 'page3_title', 'Contact', 'Kontakt');

insert into contents values(' ', 'page1_heading', 'Hello guys!', 'Hallo Leute!');
insert into contents values(' ', 'page2_heading', 'Our Products', 'Unsere Produkte');
insert into contents values(' ', 'page3_heading', 'Contact Us', 'Kontaktieren Sie uns');


On the actual website there must be a button on top of every page, so visitors can easily switch from one language to the other, that is change the language of the entire website.

Do you get what I mean? In the views the contents will be referenced using the keys from the "keyword" column. Do you think this whole thing would work...? I am not as experienced as you are...

Last edited by PixelLover (2006-10-27 17:11:45)

Re: Bilingual website

I suggested two tables to keep the number of languages dynamic so you can easily add other languages and store the selected language id in the user's session. However, it sounds like you don't need to support more than two languages, so your single-table design will probably work better.

Railscasts - Free Ruby on Rails Screencasts

Re: Bilingual website

I think http://www.globalize-rails.org/ is what you're looking for. I'm using it and it's really great It translates everything views, database, templates and controllers. It evens create a view/globalize to help you translate with a scaffolding kind of interface automagically in all language.

Re: Bilingual website

Hi,
I've made some good progress with my project, but I'm stuck again. This is a view I am using in the content management area (a split screen with the German version on the left, and the English version on the right):

<%= start_form_tag :action => 'update_content', :id => @german %>

    <table>

        <tr>
            <td></td>
            <td><h2>German</h2></td>
            <td><h2>English</h2></td>
        </tr>

        <tr>
            <td><h2>Title</h2></td>
            <td><%= text_field "german", "title" %></td>
            <td><%= text_field "english", "title" %></td>
        </tr>
   
        <tr>
            <td><h2>Body Text</h2></td>
            <td><%= text_area "german", "body" %></td>
            <td><%= text_area "english", "body" %></td>
        </tr>
   
    </table>
   
    <%= submit_tag "Save Changes" %>

<%= end_form_tag %>


And this is the controller to update the content from the form above:
  def update_content
    @german = Content.find(params[:id])
    @english = Content.find(params[:id])
    if @german.update_attributes(params[:deutsch])
      flash[:notice] = "Content successfully updated."
      redirect_to :action => 'list_contents'
    else
      render :action => 'list_contents'
    end
  end

The problem is that I need to use two variables in the form, not just "@german" as above. I can load both @german and @english into the form, but once I try to update the form fields, I get a nil object error. How can I update both @german and @english in the same form??

Last edited by PixelLover (2006-10-29 21:15:28)

Re: Bilingual website

I'm assuming "german" and "english" are just columns in the "contents" table. The only time you need to deal with columns directly is when specifying them in the view text area. The rest will take care of itself if you follow the rails conventions.

# view
<%= text_area :content, :english %>
<%= text_area :content, :german %>

# controller
@content = Content.find(params[:id])
if @content.update_attributes(params[:content])


You are only dealing with one model here (Content) not two, so you don't need to make separate  @english and @german variables.

Railscasts - Free Ruby on Rails Screencasts

Re: Bilingual website

Ok, the table I've got here is fairly simple:

create_table "contents" do |t|
    t.column "language", :string   #this can be either 'EN' or 'DE'
    t.column "rank", :int   #each rank will have a corresponding value in the other language
    t.column "title", :string
    t.column "body", :text
  end

All together there will be 4 rows in English (where 'language' = 'EN') and 4 rows in German (where 'language' = 'DE'), i.e. only 8 rows all together. How can I create a form that displays the English contents on the left, and the German contents on the right?

Last edited by PixelLover (2006-10-30 10:55:56)

Re: Bilingual website

I second the recommendation for globalize.. in combination with the globallize extensions plugin it makes dual language sites trivial to implement. (site seems down at the moment sad ) http://www.globalize-rails.org/

http://opensvn.liquid-concept.com/trac/ … _extension

Last edited by cbit (2006-10-31 07:30:40)

Re: Bilingual website

To be quite honest with you, I don't actually need the globalize plug-in because I'll have to create the content myself anyway :-). No translation software of today is actually capable of translating from one human language to the other. It still needs humans (like me) to do that.

I'll consider the globalize plug-in, if I need large amounts of error handling, user input validation etc. And that may be the case pretty soon...!