Class: Uchi::Repository::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/uchi/repository/translate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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:

  1. uchi.repository.author.button.actions
  2. uchi.common.actions
  3. "Actions"


14
15
16
17
18
19
20
# File 'lib/uchi/repository/translate.rb', line 14

def actions_button_label
  first_present_value(
    translate(i18n_scope("button.actions"), default: nil),
    translate("common.actions", default: nil),
    "Actions"
  )
end

Returns the breadcrumb label for the given page.

Example translation key:

uchi.repository.author.breadcrumb.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 breadcrumb_label(page, record: nil)
  return breadcrumb_label_for_index 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

Returns the breadcrumb label for the index page.

Returns the first of the following that is present:

  1. Translation from "uchi.repository.author.breadcrumb.index.label"
  2. Translation from "uchi.repository.author.index.title"
  3. plural name of the model
  4. Translation from "common.index"
  5. Capitalized page name ("Index")


51
52
53
54
55
56
57
58
59
# File 'lib/uchi/repository/translate.rb', line 51

def breadcrumb_label_for_index
  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

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 breadcrumb_label_for_root
  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.author.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

Returns the text for the "add" action link, used when linking to create a new record already associated with another (e.g. adding a Title from a Book's show page).

Returns the first of the following translations that is present:

  1. Translation from "uchi.repository.[name].button.link_to_add"
  2. Translation from "uchi.common.add"
  3. Default string "Add"


158
159
160
161
162
163
164
165
# File 'lib/uchi/repository/translate.rb', line 158

def link_to_add
  translate(
    "link_to_add",
    default: translate("common.add", default: "Add"),
    model: singular_name,
    scope: i18n_scope("button")
  )
end


167
168
169
# File 'lib/uchi/repository/translate.rb', line 167

def link_to_cancel
  translate("common.cancel", default: "Cancel")
end


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/uchi/repository/translate.rb', line 171

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


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/uchi/repository/translate.rb', line 190

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

Returns the text for the "new" action link.

Returns the first of the following translations that is present:

  1. Translation from "uchi.repository.[name].button.link_to_new"
  2. Translation from "uchi.common.new" with interpolation key %#model
  3. Default string "New %#model"


215
216
217
218
219
220
221
222
# File 'lib/uchi/repository/translate.rb', line 215

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



224
225
226
# File 'lib/uchi/repository/translate.rb', line 224

def loading_message
  translate("loading", default: "Loading...", scope: "uchi.repository.common")
end

Returns the label for the navigation link to this repository's index page.

Returns the first of the following that is present:

  1. Translation from "uchi.repository.[name].navigation.label"
  2. Translation from "uchi.repository.[name].index.title"
  3. plural name of the model


235
236
237
238
239
240
241
# File 'lib/uchi/repository/translate.rb', line 235

def navigation_label
  first_present_value(
    translate(i18n_scope("navigation.label"), default: nil),
    translate(i18n_scope("index.title"), default: nil),
    plural_name
  )
end

#no_records_found ⇒ Object



253
254
255
# File 'lib/uchi/repository/translate.rb', line 253

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.



245
246
247
248
249
250
251
# File 'lib/uchi/repository/translate.rb', line 245

def plural_name
  ::I18n.translate(
    "uchi.repository.#{i18n_key}.model",
    count: 2,
    default: model.model_name.plural.humanize
  )
end

#search_label ⇒ Object



257
258
259
# File 'lib/uchi/repository/translate.rb', line 257

def search_label
  translate("common.search", default: "Search")
end

#submit_button ⇒ Object

Returns the label for a generic submit button



262
263
264
# File 'lib/uchi/repository/translate.rb', line 262

def submit_button
  translate("common.save", default: "Save")
end

#successful_create ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/uchi/repository/translate.rb', line 266

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



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/uchi/repository/translate.rb', line 278

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



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/uchi/repository/translate.rb', line 290

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



302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/uchi/repository/translate.rb', line 302

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



315
316
317
318
319
320
321
322
# File 'lib/uchi/repository/translate.rb', line 315

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:

  1. Translation from "uchi.repository.[name].new.title"
  2. Translation from "uchi.repository.[name].button.link_to_new"
  3. Translation from "uchi.common.new" with interpolation key %#model
  4. Default string "New %#model"


342
343
344
345
346
347
# File 'lib/uchi/repository/translate.rb', line 342

def title_for_new
  first_present_value(
    translate(i18n_scope("new.title"), default: nil),
    link_to_new
  )
end

#title_for_show(record) ⇒ Object



324
325
326
327
328
329
330
331
332
333
# File 'lib/uchi/repository/translate.rb', line 324

def title_for_show(record)
  return repository.title(record) if record

  translate(
    "title",
    default: plural_name,
    model: singular_name,
    scope: i18n_scope(:show)
  )
end