# Delocalization

**URL:** <https://www.ruby-forum.com/t/delocalization/200563>\
**Category:** Rails I18n\
**Created:** [December 15, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/delocalization/200563 "2010-12-15T20:41:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [December 15, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/delocalization/200563/1 "2010-12-15T20:41:20Z")

</div>

Hi  
I and others have earlier discussed the need for delocalization  
(inverse translation) methods. Sven proposed a solution that did not  
work for me but for some others. As Sven said that solution was a kind  
of fix.

I have now implemented a solutions and would like to have some  
comments. The main problem is to find a solution as independen of the  
way keys are used as possible.

In my soution one call with the word to be delocalized and the method  
then makes an extended search through all translations to find the  
key,that then is used for delocalization.  
Any comments one that solution.?  
Is it to ineffective ?  
I add the code below so you can propose improvements  
I use it as an extension to the string class, but it can of course be  
used in other ways.

# Delocalizes a string.

# Delocalization means to transform a localized string to the

original used in the code, preferreble an en-string

# Searchs for the string in all translations and uses the key of

the found translation to delocalize.

# If the last word in the translation key is a dasherized version

of the string, then it is used for delocalization

# Call with to\_local= blank (or not used)

# In all other cases call with to\_locale = the language to be

delocalized to. The translation of that language will be used for  
delocalization

def to\_delocalize(to\_locale=’’)

```
 def collect_keys(scope, translations)
   full_keys = []
   translations.to_a.each do |key, translations|
     new_scope = scope.dup << key
     if translations.is_a?(Hash)
       full_keys += collect_keys(new_scope, translations)
     else
       full_keys << new_scope.join('.')
     end
   end
   return full_keys
 end
 I18n.backend.send(:init_translations)
 all_keys = I18n.backend.send(:translations).collect do |

```

check\_locale, translations|  
collect\_keys([], translations).sort  
end.flatten.uniq  
delocalized\_string=’’  
key=’’  
all\_keys.each do |key|  
result = I18n.translate(key)  
if result==self  
delocalized\_string=key.split(’.’).last  
break  
end  
end  
if !to\_locale.blank?  
I18n.locale = to\_locale.to\_sym  
delocalized\_string = I18n.translate(key)  
end  
return delocalized\_string  
end
