Riina
Riina

Reputation: 530

Gettext program

Given the phrase:

"You've successfully donated €10 to Doctors of the World"

I wanted to dynamically generate this text for different amounts and charity names, which i did using

_("You've donated %{amount} to %{charity_name}")

Where charity_name comes from a list of possible charities and each charity is a hash with data about the charity.

I'm not a french speaker and only learnt basic french in school but the problem with doing this (which is probably immediately obvious to any speaker of a language with gendered articles) is that the "to" part of the translation can take various forms a la, au, a'l or aux depending on the noun.

eg "Vous souhaitez donner 10€ aux Médecins du Monde"

What is the best way to handle this using gettext, given that this will need to be scaled to other languages? There are only a few cases where this will need to be done because most cases of dynamic text (99%+ can be handled fine with parameters.

I've thought of 3 ways to do this:

1) Have highly dynamical text such as this generated from a function, one per message per language as necessary. The function accepts an amount and charity name as a parameter and returns the translated text.

2) Manually add a translation for "to " for each charity and use that in place of %{charity_name} and then get the translation from the po file.

3) Add an entry in each charity hash specifying the form of the "to " eg the hash for les Médecins du Monde would also store aux Médecins du Monde.

Are any of these methods viable or is there a better alternative I'm not thinking off?

Upvotes: 1

Views: 125

Answers (1)

user6183958
user6183958

Reputation:

May be not the best approach but I've used this for several times.

Consider a table with below fields:

id, name_en, name_ru, created_at, updated_at

I assume that you use I18n to get language parameter.

controller

def index
  @lang = params[:locale] || I18n.locale
  @examples = Example.all
end

view

 <% @examples.each do |ex| %>
    <li>
        <%= ex.send("name_#{@lang}") %>
    </li>
 <% end %>   

Above code will present name_ru (name in russian) or name_en (in english) based of I18n.locale

Upvotes: 1

Related Questions