2

I'm getting some string into my database from an external API and I want to translate them before showing users. I don't want to translate each object by globalize3 because content that I want to translate has some format.

For example I need to change all information values with date:

"Game postponed from 22.08.2013" -> date changes but string is same.

I tried using string as key and it works if I dont have variable:

config/locale/tr.yml file

tr:

Hello World: "Merhaba Dunya"

rails console

I18n.t("Hello World")

=> "Merhaba Dunya"

How can i use my string with variable as key and get translated one?

I want to define this:

tr:

"Game postponed from %{date}" => "Mac %{date} tarihinden ertelendi"

and I want to use it by:

I18n.t("Game postponed from 22.08.2013")

=> "Mac 22.08.2013 tarihinden ertelendi"

Using I18n may not be best solution to my case but I don't know how can I solve this.

Community
  • 1
  • 1
xaph
  • 663
  • 5
  • 15

2 Answers2

1

In your config/locale/tr.yml

'Game postponed from': 'Game postponed from %{date}'

Console:

I18n.t 'Game postponed from', :date => some_date
user2503775
  • 4,267
  • 1
  • 23
  • 41
  • I know how to pass variable to I18n but my problem is I don't have a variable. "Game postponed from 22.08.2013" is a string that saved in my database. – xaph Jul 28 '13 at 15:30
  • Split the string into two parts, lookup 'Game postponed from' using I18n, and then use the date string as the variable – Slicedpan Jul 28 '13 at 15:35
  • 1
    @Slicedpan I'm looking for a general solution to this case. I've tons of different format "Game postponed ..." is just an example. I might need to localize "%{name} scored first goal" and "Game referee is %{name}". If I split them programatically I can also swap them because my site only has one language. – xaph Jul 28 '13 at 17:56
  • Is there any way in general to tell which part of each string is translatable? – Slicedpan Jul 28 '13 at 19:58
  • @Slicedpan no :( I try to create an admin panel to add formats(like "Game postponed from %{date}") and their values but I still don't know how I match them with database fields. – xaph Jul 28 '13 at 20:10
0

Here is a working version with an example:

> I18n.transliterate("arrêter".downcase.strip)
=> "arreter"
Dorian
  • 7,749
  • 4
  • 38
  • 57