# Possible to get random translation?

**URL:** <https://www.ruby-forum.com/t/possible-to-get-random-translation/186998>\
**Category:** Rails\
**Created:** [April 15, 2010, 1:08pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998 "2010-04-15T13:08:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 15, 2010, 1:08pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/1 "2010-04-15T13:08:55Z")

</div>

Hello,

does somebody know if it’s possible to get a random translation? I  
googled it and couldn’t find anything helpful.

Example:  
en:  
hello:  
0: “Hello!”  
1: “Howdy!”  
2: “G’day!”

t(“hello.#{rand(3)}”)

Any help would be appreciated! Thanks.

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 15, 2010, 1:26pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/2 "2010-04-15T13:26:43Z")

</div>

What if I create a translation model?

Example:  
t = Translation.new  
t.translation\_key = 1  
t.translation\_string = “Hello!”  
t.save  
t = Translation.new  
t.translation\_key = 1  
t.translation\_string = “Howdy!”  
t.save  
…

And get to get it Translation.first(1, :order =\> ‘RAND()’)?

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 15, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/3 "2010-04-15T18:08:40Z")

</div>

It would help to know what your larger goal is, but here’s a simple  
version:

DICTIONARY = {  
“hello” =\> [“Hello!”, “Howdy!”, “G’day!”],  
“goodbye” =\> [“Goodbye!”, “Later!”]  
}

def random\_t(string)  
variants = DICTIONARY[string]  
variants ? variants.rand : nil  
end

random\_t(“hello”)  
random\_t(“goodbye”)

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 15, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/4 "2010-04-15T19:31:33Z")

</div>

Well, the larger goal is to have hundreds if not thousands of  
internationalized strings. Your version is good for a small amount of  
strings but not applicable for me.

What would you guys prefer t(“hello.#{rand(3)}”) or Translation.first(1,  
:order =\> ‘RAND()’)?

The model would be easy to internationalize with globalize for example  
which is the same for the I18n version but rand() not variable and I’d  
need to adjust the number whenever I add another string that wouldn’t be  
the case for the translation model.

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 18, 2010, 1:20pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/5 "2010-04-18T13:20:21Z")

</div>

After some more research I decided to do it the model way. Any opinions?

---

<div class="post-metadata">

**Author:** ![cojones](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@cojones](https://www.ruby-forum.com/u/cojones)\
**Post date:** [April 20, 2010, 12:24pm UTC](https://www.ruby-forum.com/t/possible-to-get-random-translation/186998/6 "2010-04-20T12:24:54Z")

</div>

I think a model combined with globalize is the best way to store  
hundreds of strings in different languages.
