Class: Uchi::Repository::Translate
- Inherits:
-
Object
- Object
- Uchi::Repository::Translate
- Defined in:
- lib/uchi/repository/translate.rb
Instance Attribute Summary collapse
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
Instance Method Summary collapse
-
#breadcrumb_label(page, record: nil) ⇒ Object
Returns the breadcrumb label for the given page.
-
#breadcrumb_label_for_index ⇒ Object
Returns the breadcrumb label for the index page.
-
#breadcrumb_label_for_root ⇒ Object
Returns the label for the root breadcrumb item.
-
#description(page, record: nil) ⇒ Object
Returns a description for the given page, or nil if none is found.
-
#destroy_dialog_title(record = nil) ⇒ Object
Returns a title for a dialog with the given name, e.g.
- #failed_destroy ⇒ Object
-
#field_hint(field) ⇒ Object
Returns the hint for the given field, or nil if none is found.
-
#field_label(field) ⇒ Object
Returns the label for the given field.
-
#initialize(repository:) ⇒ Translate
constructor
A new instance of Translate.
- #link_to_cancel ⇒ Object
- #link_to_destroy(record) ⇒ Object
- #link_to_edit(record) ⇒ Object
-
#link_to_new ⇒ Object
Returns the text for the “new” action link.
- #loading_message ⇒ Object
-
#navigation_label ⇒ Object
Returns the label for the navigation link to this repository’s index page.
- #no_records_found ⇒ Object
-
#plural_name ⇒ Object
Returns the localized, human-readable plural name of the model this repository manages.
- #search_button ⇒ Object
- #search_label ⇒ Object
-
#submit_button ⇒ Object
Returns the label for a generic submit button.
- #successful_create ⇒ Object
- #successful_destroy ⇒ Object
- #successful_update ⇒ Object
- #title_for_edit(record) ⇒ Object
- #title_for_index ⇒ Object
-
#title_for_new ⇒ Object
Returns the title for the “new” page.
- #title_for_show(record) ⇒ Object
Constructor Details
#initialize(repository:) ⇒ Translate
Returns a new instance of Translate.
114 115 116 |
# File 'lib/uchi/repository/translate.rb', line 114 def initialize(repository:) @repository = repository end |
Instance Attribute Details
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
6 7 8 |
# File 'lib/uchi/repository/translate.rb', line 6 def repository @repository end |
Instance Method Details
#breadcrumb_label(page, record: nil) ⇒ Object
Returns the breadcrumb label for the given page.
Example translation key:
uchi.repository...edit.label
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/uchi/repository/translate.rb', line 12 def (page, record: nil) return if page.to_sym == :index default = { show: title_for_record(record) }[page.to_sym].presence default ||= translate("common.#{page}", default: page.to_s.capitalize) translate( "label", default: default, model: singular_name, record: title_for_record(record), scope: i18n_scope("breadcrumb.#{page}") ) end |
#breadcrumb_label_for_index ⇒ Object
Returns the breadcrumb label for the index page.
Returns the first of the following that is present:
-
Translation from “uchi.repository.author.breadcrumb.index.label”
-
Translation from “uchi.repository.author.index.title”
-
plural name of the model
-
Translation from “common.index”
-
Capitalized page name (“Index”)
37 38 39 40 41 42 43 44 45 |
# File 'lib/uchi/repository/translate.rb', line 37 def first_present_value( translate(i18n_scope("breadcrumb.index.label"), default: nil), translate(i18n_scope("index.title"), default: nil), plural_name, translate("common.index", default: nil), "Index" ) end |
#breadcrumb_label_for_root ⇒ Object
Returns the label for the root breadcrumb item. Defaults to the application name.
To customize this provide a translation for the key: ‘uchi.breadcrumb.root.label`
52 53 54 55 56 57 |
# File 'lib/uchi/repository/translate.rb', line 52 def first_present_value( translate("breadcrumb.root.label", default: nil), Rails.application.name.titlecase ) end |
#description(page, record: nil) ⇒ Object
Returns a description for the given page, or nil if none is found. This description is intended to provide additional context for the page being shown.
62 63 64 65 66 67 68 69 70 |
# File 'lib/uchi/repository/translate.rb', line 62 def description(page, record: nil) translate( "description", default: nil, model: singular_name, record: record, scope: i18n_scope(page) ) end |
#destroy_dialog_title(record = nil) ⇒ Object
Returns a title for a dialog with the given name, e.g. :destroy. The title may include interpolation keys such as %record.
Example translation key:
uchi.repository..dialog.destroy.title
Example default value:
Are you sure you want to delete %{record}?
Note that the default value itself is also looked up in the “common” scope, so that it can be shared across repositories.
Example fallback translation key:
common.dialog.destroy.title
Example fallback default value:
Are you sure you want to delete this record?
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/uchi/repository/translate.rb', line 89 def destroy_dialog_title(record = nil) translate( "dialog.destroy.title", default: translate( "common.dialog.destroy.title", default: "Are you sure?", record: repository.title(record) ), record: repository.title(record), scope: "uchi.repository.#{i18n_key}" ) end |
#failed_destroy ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/uchi/repository/translate.rb', line 102 def failed_destroy translate( "destroy.failure", default: translate( "destroy.failure", default: "The record could not be deleted", scope: "common" ), scope: "uchi.repository.#{i18n_key}" ) end |
#field_hint(field) ⇒ Object
Returns the hint for the given field, or nil if none is found.
128 129 130 131 132 133 134 |
# File 'lib/uchi/repository/translate.rb', line 128 def field_hint(field) translate( "hint", default: nil, scope: i18n_scope("field.#{field.name}") ) end |
#field_label(field) ⇒ Object
Returns the label for the given field.
119 120 121 122 123 124 125 |
# File 'lib/uchi/repository/translate.rb', line 119 def field_label(field) translate( "label", default: model.human_attribute_name(field.name), scope: i18n_scope("field.#{field.name}") ) end |
#link_to_cancel ⇒ Object
136 137 138 |
# File 'lib/uchi/repository/translate.rb', line 136 def link_to_cancel translate("common.cancel", default: "Cancel") end |
#link_to_destroy(record) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/uchi/repository/translate.rb', line 140 def link_to_destroy(record) first_present_value( translate( "link_to_destroy", default: nil, model: singular_name, record: repository.title(record), scope: i18n_scope("button") ), translate( "common.destroy", default: nil, model: singular_name, record: repository.title(record) ), "Delete" ) end |
#link_to_edit(record) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/uchi/repository/translate.rb', line 159 def link_to_edit(record) first_present_value( translate( "link_to_edit", default: nil, model: singular_name, record: repository.title(record), scope: i18n_scope("button") ), translate( "common.edit", default: nil, model: singular_name, record: repository.title(record) ), "Edit" ) end |
#link_to_new ⇒ Object
Returns the text for the “new” action link.
Returns the first of the following translations that is present:
-
Translation from “uchi.repository..button.link_to_new”
-
Translation from “uchi.common.new” with interpolation key %#model
-
Default string “New %#model”
184 185 186 187 188 189 190 191 |
# File 'lib/uchi/repository/translate.rb', line 184 def link_to_new translate( "link_to_new", default: translate("common.new", default: "New %{model}"), model: singular_name, scope: i18n_scope("button") ) end |
#loading_message ⇒ Object
193 194 195 |
# File 'lib/uchi/repository/translate.rb', line 193 def translate("loading", default: "Loading...", scope: "uchi.repository.common") end |
#navigation_label ⇒ Object
Returns the label for the navigation link to this repository’s index page.
Returns the first of the following that is present:
-
Translation from “uchi.repository..navigation.label”
-
Translation from “uchi.repository..index.title”
-
plural name of the model
204 205 206 207 208 209 210 |
# File 'lib/uchi/repository/translate.rb', line 204 def first_present_value( translate(i18n_scope("navigation.label"), default: nil), translate(i18n_scope("index.title"), default: nil), plural_name ) end |
#no_records_found ⇒ Object
222 223 224 |
# File 'lib/uchi/repository/translate.rb', line 222 def no_records_found translate("no_records_found", default: "No records found", scope: "uchi.common") end |
#plural_name ⇒ Object
Returns the localized, human-readable plural name of the model this repository manages.
214 215 216 217 218 219 220 |
# File 'lib/uchi/repository/translate.rb', line 214 def plural_name ::I18n.translate( "uchi.repository.#{i18n_key}.model", count: 2, default: model.model_name.plural.humanize ) end |
#search_button ⇒ Object
230 231 232 |
# File 'lib/uchi/repository/translate.rb', line 230 def translate("common.search", default: "Search") end |
#search_label ⇒ Object
226 227 228 |
# File 'lib/uchi/repository/translate.rb', line 226 def search_label translate("common.search", default: "Search") end |
#submit_button ⇒ Object
Returns the label for a generic submit button
235 236 237 |
# File 'lib/uchi/repository/translate.rb', line 235 def translate("common.save", default: "Save") end |
#successful_create ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/uchi/repository/translate.rb', line 239 def successful_create translate( "create.success", default: translate( "create.success", default: "Your changes have been saved", scope: "common" ), scope: "uchi.repository.#{i18n_key}" ) end |
#successful_destroy ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/uchi/repository/translate.rb', line 251 def successful_destroy translate( "destroy.success", default: translate( "destroy.success", default: "The record has been deleted", scope: "common" ), scope: "uchi.repository.#{i18n_key}" ) end |
#successful_update ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/uchi/repository/translate.rb', line 263 def successful_update translate( "update.success", default: translate( "update.success", default: "Your changes have been saved", scope: "common" ), scope: "uchi.repository.#{i18n_key}" ) end |
#title_for_edit(record) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/uchi/repository/translate.rb', line 275 def title_for_edit(record) first_present_value( translate( "title", default: nil, model: singular_name, record: repository.title(record), scope: i18n_scope(:edit) ), link_to_edit(record) ) end |
#title_for_index ⇒ Object
288 289 290 291 292 293 294 295 |
# File 'lib/uchi/repository/translate.rb', line 288 def title_for_index translate( "title", default: plural_name, model: singular_name, scope: i18n_scope(:index) ) end |
#title_for_new ⇒ Object
Returns the title for the “new” page.
Returns the first of the following translations that is present:
-
Translation from “uchi.repository..new.title”
-
Translation from “uchi.repository..button.link_to_new”
-
Translation from “uchi.common.new” with interpolation key %#model
-
Default string “New %#model”
315 316 317 318 319 320 |
# File 'lib/uchi/repository/translate.rb', line 315 def title_for_new first_present_value( translate(i18n_scope("new.title"), default: nil), link_to_new ) end |
#title_for_show(record) ⇒ Object
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/uchi/repository/translate.rb', line 297 def title_for_show(record) return repository.title(record) if record translate( "title", default: plural_name, model: singular_name, scope: i18n_scope(:show) ) end |