-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a built-in, dependency free localization adapter (#238)
* Implement a built-in localization adapter The built-in adapter is trivial and zero-deps. This is thought to be the first step of a two step update. **First step** (This one) The gem ships a localization adapter that does not rely on i18n gem. It has not feature parity because it can't interpolate variables. The gem, when loaded, will check for `I18n` presence into the global space. If it's not present, then it will use the buil-in adapter. If it is present, then an alternative LightService::I18n::LocalizationAdapter will be used. LightService::I18n::LocalizationAdapter is exaclty the localization adapter shipped previously. The README is updated to reflect the new scenario. Keep in mind that as it stands, i18n gem is required indirectly by light-service as an activesupport dependency. This means that this PR won't affect any implementation. **Step two** LightService::I18n::LocalizationAdapter and its concerning specs will be moved into a new `light_service-i18n` gem (or `light_service-translation`). The gem will stop to automatically choose the translation adapter, always defaulting to the built-in one. light_service-i18n will instead forcibly configure itself as translation adapter. Releasing this step will require to bump the major version and in release notes will be noticed that in order to preserve previous i18n based translation logic will require to add the new gem to the implementor's Gemfile. Alternatively, a new branch (`zero-deps`, e.g.) will be created on the original repository and the PR containing "step two" will be pointed to that "bridge" branch. The major version bump could this way be delayed to the future when generators will be migrated to dedicated gem and the current inflector (activesupport) will be changed to something else. * Update README.md Co-authored-by: Attila Domokos <[email protected]> * Update README.md Co-authored-by: Attila Domokos <[email protected]> Co-authored-by: Attila Domokos <[email protected]>
- Loading branch information
1 parent
5ac824a
commit bc5dcc0
Showing
9 changed files
with
278 additions
and
99 deletions.
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,46 @@ | ||
module LightService | ||
module I18n | ||
class LocalizationAdapter | ||
def failure(message_or_key, action_class, i18n_options = {}) | ||
find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
:type => :failure) | ||
end | ||
|
||
def success(message_or_key, action_class, i18n_options = {}) | ||
find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
:type => :success) | ||
end | ||
|
||
private | ||
|
||
def find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
type) | ||
if message_or_key.is_a?(Symbol) | ||
i18n_options.merge!(type) | ||
translate(message_or_key, action_class, i18n_options) | ||
else | ||
message_or_key | ||
end | ||
end | ||
|
||
def translate(key, action_class, options = {}) | ||
type = options.delete(:type) | ||
|
||
scope = i18n_scope_from_class(action_class, type) | ||
options[:scope] = scope | ||
|
||
::I18n.t(key, **options) | ||
end | ||
|
||
def i18n_scope_from_class(action_class, type) | ||
"#{action_class.name.underscore}.light_service.#{type.to_s.pluralize}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,31 @@ | ||
module LightService | ||
class LocalizationAdapter | ||
def failure(message_or_key, action_class, i18n_options = {}) | ||
def failure(message_or_key, action_class) | ||
find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
:type => :failure) | ||
action_class.to_s.underscore, | ||
:failures) | ||
end | ||
|
||
def success(message_or_key, action_class, i18n_options = {}) | ||
def success(message_or_key, action_class) | ||
find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
:type => :success) | ||
action_class.to_s.underscore, | ||
:successes) | ||
end | ||
|
||
private | ||
|
||
def find_translated_message(message_or_key, | ||
action_class, | ||
i18n_options, | ||
type) | ||
def find_translated_message(message_or_key, action_class, type) | ||
if message_or_key.is_a?(Symbol) | ||
i18n_options.merge!(type) | ||
translate(message_or_key, action_class, i18n_options) | ||
LightService::LocalizationMap.instance.dig( | ||
LightService::Configuration.locale, | ||
action_class.to_sym, | ||
:light_service, | ||
type, | ||
message_or_key | ||
) | ||
else | ||
message_or_key | ||
end | ||
end | ||
|
||
def translate(key, action_class, options = {}) | ||
type = options.delete(:type) | ||
|
||
scope = i18n_scope_from_class(action_class, type) | ||
options[:scope] = scope | ||
|
||
I18n.t(key, **options) | ||
end | ||
|
||
def i18n_scope_from_class(action_class, type) | ||
"#{action_class.name.underscore}.light_service.#{type.to_s.pluralize}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'singleton' | ||
|
||
module LightService | ||
class LocalizationMap < Hash | ||
include ::Singleton | ||
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
Oops, something went wrong.