如何遍历 Rails 4 中所有可用的 I18n 变量

How to loop through all available I18n variables in Rails 4

我在 application.rb

中定义了一定数量的可用 I18n 语言环境

config/application.rb

...
config.i18n.available_locales = [:at, :de, :ch]
...

我需要在某些控制器操作中同时手动使所有语言的缓存过期。有没有办法遍历所有语言?

最终并没有那么难,我 miss-spelt 我认为一定是这样的方法,因此每次都是错误的。如果您遇到同样的问题,请查看 Rails 控制台中的 I18n.methods。

回答问题:

irb(main):001:0> I18n.methods
=> [:interpolate, :interpolate_hash, :fallbacks, :fallbacks=, :config, :config=, :locale, :locale=, :backend, :backend=, :default_locale, :default_locale=, :available_locales, :available_locales=, :default_separator, :default_separator=, :exception_handler, :exception_handler=, :load_path, :load_path=, :enforce_available_locales, :enforce_available_locales=, :reload!, :translate, :t, :translate!, :t!, :exists?, :transliterate, :localize, :l, :with_locale, :normalize_keys, :locale_available?, :enforce_available_locales!, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, :include, :prepend, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :mattr_reader, :cattr_reader, :mattr_writer, :cattr_writer, :mattr_accessor, :cattr_accessor, :delegate, :remove_possible_method, :redefine_method, :alias_method_chain, :alias_attribute, :parent_name, :parent, :parents, :local_constants, :deprecate, :attr_internal_reader, :attr_internal_writer, :attr_internal_accessor, :attr_internal, :anonymous?, :reachable?, :qualified_const_defined?, :qualified_const_get, :qualified_const_set, :psych_yaml_as, :yaml_as, :rake_extension, :methods_transplantable?, :pretty_print_cycle, :pretty_print, :trace_execution_scoped, :trace_execution_unscoped, :trace_method_execution, :trace_method_execution_with_scope, :trace_method_execution_no_scope, :get_stats_scoped, :get_stats_unscoped, :add_method_tracer, :remove_method_tracer, :check_for_illegal_keys!, :check_for_push_scope_and_metric, :validate_options, :default_metric_name_code, :newrelic_method_exists?, :traced_method_exists?, :assemble_code_header, :method_without_push_scope, :method_with_push_scope, :code_to_eval, :const_missing, :guess_for_anonymous, :unloadable, :concerning, :concern, :blank?, :present?, :presence, :acts_like?, :duplicable?, :deep_dup, :try, :try!, :in?, :presence_in, :to_param, :to_query, :instance_values, :instance_variable_names, :to_json_with_active_support_encoder, :to_json_without_active_support_encoder, :to_json, :as_json, :with_options, :html_safe?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :ya2yaml, :is_haml?, :`, :pretty_print_instance_variables, :pretty_print_inspect, :friendly_id?, :unfriendly_id?, :require_or_load, :require_dependency, :load_dependency, :load, :require, :to_v8, :to_ruby, :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :gem, :silence_warnings, :enable_warnings, :with_warnings, :silence_stderr, :silence_stream, :suppress, :capture, :silence, :quietly, :suppress_warnings, :pretty_inspect, :debugger, :breakpoint, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main):002:0> I18n.available_locales
=> [:at, :de, :ch]

所以 available_locales 是我(也许还有你)所需要的。