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.



114
115
116
# File 'lib/uchi/repository/translate.rb', line 114

def initialize(repository:)
  @repository = repository
end

Instance Attribute Details

#repositoryObject (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

Returns the breadcrumb label for the given page.

Example translation key:

uchi.repository.author.breadcrumb.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 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”)



37
38
39
40
41
42
43
44
45
# File 'lib/uchi/repository/translate.rb', line 37

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`



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

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.



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.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?


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_destroyObject



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


136
137
138
# File 'lib/uchi/repository/translate.rb', line 136

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


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


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

Returns the text for the “new” action link.

Returns the first of the following translations that is present:

  1. Translation from “uchi.repository..button.link_to_new”

  2. Translation from “uchi.common.new” with interpolation key %#model

  3. 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_messageObject



193
194
195
# File 'lib/uchi/repository/translate.rb', line 193

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..navigation.label”

  2. Translation from “uchi.repository..index.title”

  3. plural name of the model



204
205
206
207
208
209
210
# File 'lib/uchi/repository/translate.rb', line 204

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_foundObject



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_nameObject

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_buttonObject



230
231
232
# File 'lib/uchi/repository/translate.rb', line 230

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

#search_labelObject



226
227
228
# File 'lib/uchi/repository/translate.rb', line 226

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

#submit_buttonObject

Returns the label for a generic submit button



235
236
237
# File 'lib/uchi/repository/translate.rb', line 235

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

#successful_createObject



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_destroyObject



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_updateObject



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_indexObject



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_newObject

Returns the title for the “new” page.

Returns the first of the following translations that is present:

  1. Translation from “uchi.repository..new.title”

  2. Translation from “uchi.repository..button.link_to_new”

  3. Translation from “uchi.common.new” with interpolation key %#model

  4. 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