Class: RailsTranslateRoutes

Inherits:
Object
  • Object
show all
Includes:
DictionaryManagement, Translator
Defined in:
lib/rails-translate-routes.rb

Overview

This class knows nothing about Rails.root or Rails.application.routes, and therefore is easier to test without a Rails app.

Defined Under Namespace

Modules: DictionaryManagement, Translator

Constant Summary collapse

TRANSLATABLE_SEGMENT =
/^([\w-]+)(\()?/.freeze
LOCALE_PARAM_KEY =
:locale.freeze
ROUTE_HELPER_CONTAINER =
[
  ActionController::Base,
  ActionView::Base,
  ActionMailer::Base,
  ActionDispatch::Routing::UrlFor
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Translator

#add_prefix?, #add_root_route, #add_untranslated_helpers_to_controllers_and_views, #translate, #translate_path, #translate_path_segment, #translate_route, #translate_string, #translations_for, #untranslated_route

Methods included from DictionaryManagement

#add_dictionary_from_file, #init_i18n_dictionary, #load_dictionary_from_file, #merge_translations, #yield_dictionary

Instance Attribute Details

#dictionaryObject

Attributes



17
18
19
# File 'lib/rails-translate-routes.rb', line 17

def dictionary
  @dictionary
end

Class Method Details

.init_from_file(file_path) ⇒ Object

Creates a RailsTranslateRoutes instance and reads the translations from a specified file



94
95
96
97
98
# File 'lib/rails-translate-routes.rb', line 94

def init_from_file file_path
  new.tap do |t|
    t.load_dictionary_from_file file_path
  end
end

.init_with_i18n(*wanted_locales) ⇒ Object

Creates a RailsTranslateRoutes instance, using I18n dictionaries of your app



78
79
80
81
82
# File 'lib/rails-translate-routes.rb', line 78

def init_with_i18n *wanted_locales
  new.tap do |t|
    t.init_i18n_dictionary *wanted_locales
  end
end

.init_with_yield(&block) ⇒ Object

Creates a RailsTranslateRoutes instance and evaluates given block with an empty dictionary



86
87
88
89
90
# File 'lib/rails-translate-routes.rb', line 86

def init_with_yield &block
  new.tap do |t|
    t.yield_dictionary &block
  end
end

.locale_suffix(locale) ⇒ Object

Default locale suffix generator



72
73
74
# File 'lib/rails-translate-routes.rb', line 72

def locale_suffix locale
  locale.to_s.underscore
end

Instance Method Details

#available_localesObject



19
20
21
# File 'lib/rails-translate-routes.rb', line 19

def available_locales
  @available_locales ||= I18n.available_locales.map(&:to_s)
end

#available_locales=(locales) ⇒ Object



23
24
25
# File 'lib/rails-translate-routes.rb', line 23

def available_locales= locales
  @available_locales = locales.map(&:to_s)
end

#default_localeObject



27
28
29
# File 'lib/rails-translate-routes.rb', line 27

def default_locale
  @default_locale ||= I18n.default_locale.to_s
end

#default_locale=(locale) ⇒ Object



31
32
33
# File 'lib/rails-translate-routes.rb', line 31

def default_locale= locale
  @default_locale = locale.to_s
end

#default_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rails-translate-routes.rb', line 35

def default_locale? locale
  default_locale == locale.to_s
end

#keep_untranslated_routesObject

option allowing to keep untranslated routes *Ex:

*resources :users
*translated routes
  en/members
  fr/membres
  /users


62
63
64
# File 'lib/rails-translate-routes.rb', line 62

def keep_untranslated_routes
  @keep_untranslated_routes ||= false
end

#keep_untranslated_routes=(keep_untranslated_routes) ⇒ Object



66
67
68
# File 'lib/rails-translate-routes.rb', line 66

def keep_untranslated_routes= keep_untranslated_routes
  @keep_untranslated_routes = keep_untranslated_routes
end

#locale_suffix(locale) ⇒ Object



377
378
379
# File 'lib/rails-translate-routes.rb', line 377

def locale_suffix locale
  self.class.locale_suffix locale
end

#no_prefixesObject



47
48
49
# File 'lib/rails-translate-routes.rb', line 47

def no_prefixes
  @no_prefixes ||= false
end

#no_prefixes=(no_prefixes) ⇒ Object



51
52
53
# File 'lib/rails-translate-routes.rb', line 51

def no_prefixes= no_prefixes
  @no_prefixes = no_prefixes
end

#prefix_on_default_localeObject



39
40
41
# File 'lib/rails-translate-routes.rb', line 39

def prefix_on_default_locale
  @prefix_on_default_locale ||= I18n.default_locale.to_s
end

#prefix_on_default_locale=(locale) ⇒ Object



43
44
45
# File 'lib/rails-translate-routes.rb', line 43

def prefix_on_default_locale= locale
  @prefix_on_default_locale = locale.to_s
end