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
-
#actions_button_label ⇒ Object
Returns the label for the actions button in the UI.
-
#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.
128 129 130 |
# File 'lib/uchi/repository/translate.rb', line 128 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
#actions_button_label ⇒ Object
Returns the label for the actions button in the UI.
Returns the first of the following that is present:
-
uchi.repository.author.button.actions
-
uchi.common.actions
-
“Actions”
14 15 16 17 18 19 20 |
# File 'lib/uchi/repository/translate.rb', line 14 def first_present_value( translate(i18n_scope("button.actions"), default: nil), translate("common.actions", default: nil), "Actions" ) end |
#breadcrumb_label(page, record: nil) ⇒ Object
Returns the breadcrumb label for the given page.
Example translation key:
uchi.repository...edit.label
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/uchi/repository/translate.rb', line 26 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”)
51 52 53 54 55 56 57 58 59 |
# File 'lib/uchi/repository/translate.rb', line 51 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
66 67 68 69 70 71 |
# File 'lib/uchi/repository/translate.rb', line 66 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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/uchi/repository/translate.rb', line 76 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?
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/uchi/repository/translate.rb', line 103 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
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/uchi/repository/translate.rb', line 116 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.
142 143 144 145 146 147 148 |
# File 'lib/uchi/repository/translate.rb', line 142 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.
133 134 135 136 137 138 139 |
# File 'lib/uchi/repository/translate.rb', line 133 def field_label(field) translate( "label", default: model.human_attribute_name(field.name), scope: i18n_scope("field.#{field.name}") ) end |
#link_to_cancel ⇒ Object
150 151 152 |
# File 'lib/uchi/repository/translate.rb', line 150 def link_to_cancel translate("common.cancel", default: "Cancel") end |
#link_to_destroy(record) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/uchi/repository/translate.rb', line 154 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/uchi/repository/translate.rb', line 173 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.[name].button.link_to_new”
-
Translation from “uchi.common.new” with interpolation key %#model
-
Default string “New %#model”
198 199 200 201 202 203 204 205 |
# File 'lib/uchi/repository/translate.rb', line 198 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
207 208 209 |
# File 'lib/uchi/repository/translate.rb', line 207 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.[name].navigation.label”
-
Translation from “uchi.repository.[name].index.title”
-
plural name of the model
218 219 220 221 222 223 224 |
# File 'lib/uchi/repository/translate.rb', line 218 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
236 237 238 |
# File 'lib/uchi/repository/translate.rb', line 236 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.
228 229 230 231 232 233 234 |
# File 'lib/uchi/repository/translate.rb', line 228 def plural_name ::I18n.translate( "uchi.repository.#{i18n_key}.model", count: 2, default: model.model_name.plural.humanize ) end |
#search_button ⇒ Object
244 245 246 |
# File 'lib/uchi/repository/translate.rb', line 244 def translate("common.search", default: "Search") end |
#search_label ⇒ Object
240 241 242 |
# File 'lib/uchi/repository/translate.rb', line 240 def search_label translate("common.search", default: "Search") end |
#submit_button ⇒ Object
Returns the label for a generic submit button
249 250 251 |
# File 'lib/uchi/repository/translate.rb', line 249 def translate("common.save", default: "Save") end |
#successful_create ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/uchi/repository/translate.rb', line 253 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
265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/uchi/repository/translate.rb', line 265 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
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/uchi/repository/translate.rb', line 277 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
289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/uchi/repository/translate.rb', line 289 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
302 303 304 305 306 307 308 309 |
# File 'lib/uchi/repository/translate.rb', line 302 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.[name].new.title”
-
Translation from “uchi.repository.[name].button.link_to_new”
-
Translation from “uchi.common.new” with interpolation key %#model
-
Default string “New %#model”
329 330 331 332 333 334 |
# File 'lib/uchi/repository/translate.rb', line 329 def title_for_new first_present_value( translate(i18n_scope("new.title"), default: nil), link_to_new ) end |
#title_for_show(record) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/uchi/repository/translate.rb', line 311 def title_for_show(record) return repository.title(record) if record translate( "title", default: plural_name, model: singular_name, scope: i18n_scope(:show) ) end |