config.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. module I18n
  2. class Config
  3. # The only configuration value that is not global and scoped to thread is :locale.
  4. # It defaults to the default_locale.
  5. def locale
  6. @locale ||= default_locale
  7. end
  8. # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
  9. def locale=(locale)
  10. @locale = locale.to_sym rescue nil
  11. end
  12. # Returns the current backend. Defaults to +Backend::Simple+.
  13. def backend
  14. @@backend ||= Backend::Simple.new
  15. end
  16. # Sets the current backend. Used to set a custom backend.
  17. def backend=(backend)
  18. @@backend = backend
  19. end
  20. # Returns the current default locale. Defaults to :'en'
  21. def default_locale
  22. @@default_locale ||= :en
  23. end
  24. # Sets the current default locale. Used to set a custom default locale.
  25. def default_locale=(locale)
  26. @@default_locale = locale.to_sym rescue nil
  27. end
  28. # Returns an array of locales for which translations are available.
  29. # Unless you explicitely set these through I18n.available_locales=
  30. # the call will be delegated to the backend.
  31. def available_locales
  32. @@available_locales ||= nil
  33. @@available_locales || backend.available_locales
  34. end
  35. # Sets the available locales.
  36. def available_locales=(locales)
  37. @@available_locales = Array(locales).map { |locale| locale.to_sym }
  38. @@available_locales = nil if @@available_locales.empty?
  39. end
  40. # Returns the current default scope separator. Defaults to '.'
  41. def default_separator
  42. @@default_separator ||= '.'
  43. end
  44. # Sets the current default scope separator.
  45. def default_separator=(separator)
  46. @@default_separator = separator
  47. end
  48. # Return the current exception handler. Defaults to :default_exception_handler.
  49. def exception_handler
  50. @@exception_handler ||= ExceptionHandler.new
  51. end
  52. # Sets the exception handler.
  53. def exception_handler=(exception_handler)
  54. @@exception_handler = exception_handler
  55. end
  56. # Allow clients to register paths providing translation data sources. The
  57. # backend defines acceptable sources.
  58. #
  59. # E.g. the provided SimpleBackend accepts a list of paths to translation
  60. # files which are either named *.rb and contain plain Ruby Hashes or are
  61. # named *.yml and contain YAML data. So for the SimpleBackend clients may
  62. # register translation files like this:
  63. # I18n.load_path << 'path/to/locale/en.yml'
  64. def load_path
  65. @@load_path ||= []
  66. end
  67. # Sets the load path instance. Custom implementations are expected to
  68. # behave like a Ruby Array.
  69. def load_path=(load_path)
  70. @@load_path = load_path
  71. end
  72. end
  73. end