module Mobility
Constants
- CALL_COMPILABLE_REGEXP
Public Class Methods
Returns available locales. Defaults to I18n.available_locales, but will use Rails.application.config.i18n.available_locales if Rails is loaded and config is non-nil. @return [Array<Symbol>] Available locales @note The special case for Rails is necessary due to the fact that Rails
may load the model before setting +I18n.available_locales+. If we simply default to +I18n.available_locales+, we may define many more methods (in LocaleAccessors) than is really necessary.
# File lib/mobility.rb, line 229 def available_locales if defined?(Rails) && Rails.respond_to?(:application) && Rails.application Rails.application.config.i18n.available_locales&.map(&:to_sym) || I18n.available_locales else I18n.available_locales end end
Configure Mobility
@yield [Mobility::Translations]
# File lib/mobility.rb, line 117 def configure(&block) translates_with(Class.new(Translations)) unless @translations_class if block.arity == 0 translations_class.instance_exec(&block) else yield translations_class end end
Alias to default backend defined on *translations_class+. @return [Symbol,Class]
# File lib/mobility.rb, line 111 def default_backend translations_class.defaults[:backend]&.first end
Raises InvalidLocale
exception if the locale passed in is present but not available. @param [String,Symbol] locale @raise [InvalidLocale] if locale is present but not available
# File lib/mobility.rb, line 217 def enforce_available_locales!(locale) raise Mobility::InvalidLocale.new(locale) unless (locale.nil? || available_locales.include?(locale.to_sym)) end
# File lib/mobility.rb, line 96 def extended(model_class) def model_class.translates(*args, **options) include Mobility.translations_class.new(*args, **options) end end
# File lib/mobility/version.rb, line 4 def self.gem_version Gem::Version.new VERSION::STRING end
Extends model with this class so that +include Mobility+ is equivalent to +extend Mobility+ (but extend
is preferred). @param model_class
# File lib/mobility.rb, line 105 def included(model_class) model_class.extend self end
@!group Locale Accessors @return [Symbol] Mobility
locale
# File lib/mobility.rb, line 143 def locale read_locale || I18n.locale end
Sets Mobility
locale @param [Symbol] locale Locale to set @raise [InvalidLocale] if locale is nil or not in
+Mobility.available_locales+ (if +I18n.enforce_available_locales+ is +true+)
@return [Symbol] Locale
# File lib/mobility.rb, line 152 def locale=(locale) set_locale(locale) end
Return normalized locale @param [String,Symbol] locale @return [String] Normalized locale @example
Mobility.normalize_locale(:ja) #=> "ja" Mobility.normalize_locale("pt-BR") #=> "pt_br"
# File lib/mobility.rb, line 183 def normalize_locale(locale = Mobility.locale) "#{locale.to_s.downcase.tr("-", "_")}" end
Return normalized locale accessor name @param [String,Symbol] attribute @param [String,Symbol] locale @return [String] Normalized locale accessor name @raise [ArgumentError] if generated accessor has an invalid format @example
Mobility.normalize_locale_accessor(:foo, :ja) #=> "foo_ja" Mobility.normalize_locale_accessor(:bar, "pt-BR") #=> "bar_pt_br"
# File lib/mobility.rb, line 198 def normalize_locale_accessor(attribute, locale = Mobility.locale) "#{attribute}_#{normalize_locale(locale)}".tap do |accessor| unless CALL_COMPILABLE_REGEXP.match(accessor) raise ArgumentError, "#{accessor.inspect} is not a valid accessor" end end end
# File lib/mobility.rb, line 137 def reset_translations_class @translations_class = nil end
@return [RequestStore] Request store
# File lib/mobility.rb, line 171 def storage RequestStore.store end
# File lib/mobility.rb, line 126 def translates_with(pluggable) raise ArgumentError, "translations class must be a subclass of Module." unless Module === pluggable @translations_class = pluggable end
# File lib/mobility.rb, line 131 def translations_class @translations_class || raise(Error, "Mobility has not been configured. "\ "Configure with Mobility.configure, or assign a translations class with Mobility.translates_with(<class>)") end
Check that a non-nil locale is valid. (Does not actually parse locale to check its format.) @raise [InvalidLocale] if locale is not a Symbol or not available
# File lib/mobility.rb, line 209 def validate_locale!(locale) raise Mobility::InvalidLocale.new(locale) unless Symbol === locale enforce_available_locales!(locale) if I18n.enforce_available_locales end
Sets Mobility
locale around block @param [Symbol] locale Locale to set in block @yield [Symbol] Locale
# File lib/mobility.rb, line 159 def with_locale(locale) previous_locale = read_locale begin set_locale(locale) yield(locale) ensure set_locale(previous_locale) end end
Protected Class Methods
# File lib/mobility.rb, line 239 def read_locale storage[:mobility_locale] end
# File lib/mobility.rb, line 243 def set_locale(locale) locale = locale.to_sym if String === locale validate_locale!(locale) if locale storage[:mobility_locale] = locale end