-
-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce support for deprecating translation keys
Adds an `I18n::deprecate` API that allows users to mark a key as deprecated and superseded by another. `I18n::t` will then use the following logic: - If the key supersedes a deprecated one, and the old one is still defined, use the old one instead. If the old one is not defined anymore, use the new one. - If the key is deprecated and the old one is still defined, use the old one. If the old one is not defined anymore, use the new one. - For all other keys, regular behavior is maintained. The reason the old key is always preferred to the new one when available is that users may have been using the new key for other purposes, and we don't want to swap it for the old one without notice. Instead, users have to manually go in and delete the old key for the new one to start being used.
- Loading branch information
1 parent
2942126
commit 981b12d
Showing
6 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n | ||
module Backend | ||
module Deprecator | ||
def store_translations(locale, data, options = EMPTY_HASH) | ||
super | ||
|
||
I18n.config.deprecations.each do |deprecated_key, new_key| | ||
if lookup(locale, deprecated_key) | ||
puts "DEPRECATION WARNING: #{deprecated_key} is deprecated, use #{new_key} instead" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'test_helper' | ||
|
||
class I18nBackendDeprecatorTest < I18n::TestCase | ||
class Backend < I18n::Backend::Simple | ||
include I18n::Backend::Deprecator | ||
include I18n::Backend::Fallbacks | ||
end | ||
|
||
def setup | ||
super | ||
I18n.backend = Backend.new | ||
I18n.deprecate(old_key: :new_key) | ||
end | ||
|
||
test 'old key defined, new key undefined' do | ||
store_translations(:xx, old_key: 'Hello world') | ||
|
||
assert_equal 'Hello world', I18n.t(:old_key, locale: :xx) | ||
assert_equal 'Hello world', I18n.t(:new_key, locale: :xx) | ||
end | ||
|
||
test 'old key undefined, new key defined' do | ||
store_translations(:xx, new_key: 'Hello new world') | ||
|
||
assert_equal 'Hello new world', I18n.t(:old_key, locale: :xx) | ||
assert_equal 'Hello new world', I18n.t(:new_key, locale: :xx) | ||
end | ||
|
||
test 'old key defined, new key defined' do | ||
store_translations(:xx, old_key: 'Hello world', new_key: 'Hello new world') | ||
|
||
assert_equal 'Hello world', I18n.t(:old_key, locale: :xx) | ||
assert_equal 'Hello world', I18n.t(:new_key, locale: :xx) | ||
end | ||
end |