svenfuchs/i18n — pull request #274 by Chipairon (2014-07-21 15:14:33+00:00)
Title: Add tests to check if each backend can support store/lookup of keys ending in the separator character ("." by default).

There is an inconsistency about how each backend treats the storage and retrieval of keys ending in the scope separator special character (a dot by default):

**Simple backend:**

```
store_translations :'en', :"yyy." => "yyy value" # Stores { :"yyy." => "yyy value" }
I18n.t("yyy.") # translation missing "en.yyy"
```

Translation misses because the code at https://github.com/svenfuchs/i18n/blob/b5703d7431ecfc1b99409e16c6590448570718ac/lib/i18n.rb#L329 strips the final "." from the key while looking up.

**KeyValue backend:**

```
store_translations :'en', :"yyy." => "yyy value" # Stores { "en.yyy\x01" => "yyy value" }
I18n.t("yyy.") # translation missing "en.yyy."
```

Lookup misses because the code at https://github.com/svenfuchs/i18n/blob/b5703d7431ecfc1b99409e16c6590448570718ac/lib/i18n/backend/flatten.rb#L18 does not scape the final '.' as it does on storing. I have tested this patch to make this backend able to scape the key on lookup as it does on storing:

# This will go at https://github.com/svenfuchs/i18n/blob/b5703d7431ecfc1b99409e16c6590448570718ac/lib/i18n/backend/flatten.rb#L28
if keys.last.to_s.end_with? separator
was_a_symbol = keys.last.is_a? Symbol
keys[keys.count - 1] = escape_default_separator(keys.last)
keys[keys.count - 1] = keys.last.to_sym if was_a_symbol
end

**Simple with Memoize backend:**

Lookup misses in a similar way than Simple, but the data is inconsistent, because the code on
https://github.com/svenfuchs/i18n/blob/b5703d7431ecfc1b99409e16c6590448570718ac/lib/i18n/backend/memoize.rb#L33 causes this difference in the 'stores':
Memoized 'store' --> `@memoized_lookup = { :en => { :"yyy." => nil } } `
While Simple 'store' contains --> `@translations = { :en => { :"yyy." => "yyy value" } } `

This is because the lookup code at memoized module (the line linked above) does not strip the last ".", as Simple lookup does.

If someone has some suggestion about how to tackle this and gives me some pointers, I will be happy to contribute. Should this code be refactored as part of the base backend? Also, this tests could be moved to https://github.com/svenfuchs/i18n/blob/master/lib/i18n/tests/lookup.rb so it is not repeated on every backend, but I am not sure about this.