Create locale helper for global translations

There are some translation strings we should use across multiple views.
Instead of duplicating the translations, we can use a different locale
helper method, `#tt`, to get a hash of all available translations.

Then, in a partial view like `pagination.mustache`, we can render
translations regardless of what the current view class is.

This commit adds the necessary helper, tests, and uses the new method to
render translations on the `pagination.mustache` template, which is used
by many other view classes (latest changes, history, and search).
This commit is contained in:
benjamin wil
2022-05-07 20:13:37 -07:00
parent 33ed562d1c
commit bc490c64e5
4 changed files with 114 additions and 19 deletions
+7 -14
View File
@@ -1,4 +1,11 @@
en:
pagination:
aria:
label: Pagination
next_page: Next page
previous_page: Previous page
next: Next
previous: Previous
precious/views/compare:
back_to_page_history: Back to Page History
back_to_top: Back to Top
@@ -12,13 +19,6 @@ en:
browse_in_history_description: Browse the page at this point in the history
compare_revisions: Compare Revisions
history_for: History for
pagination:
aria:
label: Pagination
next_page: Next page
previous_page: Previous page
next: Next
previous: Previous
precious/views/latest_changes:
title: Latest Changes (Globally)
precious/views/layout:
@@ -34,12 +34,5 @@ en:
show_all: "Show all %{count} hits in this page"
back_to_top: Back to Top
no_results: There are no results for your search
pagination:
aria:
label: Pagination
next_page: Next page
previous_page: Previous page
next: Next
previous: Previous
search_results_for: Search results for
title: "Search results for %{query}"
+5 -5
View File
@@ -1,23 +1,23 @@
<nav class="paginate-container" aria-label="{{t.pagination.aria.label}}">
<nav class="paginate-container" aria-label="{{tt.pagination.aria.label}}">
<div class="pagination" id="pagination">
<a
aria-label="{{t.pagination.aria.previous_page}}"
aria-label="{{td.pagination.aria.previous_page}}"
class="previous_page {{^previous_page}}disabled{{/previous_page}}"
href="?page_num={{previous_page}}{{query_string}}"
id="prev"
rel="prev"
>
{{t.pagination.previous}}
{{tt.pagination.previous}}
</a>
<a
aria-label="{{t.pagination.aria.next_page}}"
aria-label="{{td.pagination.aria.next_page}}"
class="next_page {{^next_page}}disabled{{/next_page}}"
href="?page_num={{next_page}}{{query_string}}"
id="next"
rel="next"
>
{{t.pagination.next}}
{{tt.pagination.next}}
</a>
</div>
</nav>
@@ -21,6 +21,13 @@ module Precious
autofill I18n.t(locale_klass_name)
end
# Returns all I18n translation strings from the root of an I18n YAML file.
# Otherwise, it works exactly like the `#t` method that's also defined in
# this file.
def tt
autofill I18n.t('.')
end
private
# Recursively looks up I18n translation values and autofills any YAML
+95
View File
@@ -132,4 +132,99 @@ describe Precious::Views::LocaleHelpers do
end
end
end
describe "#tt" do
describe "mustache usage" do
let(:subject) { dummy_instance.render(mustache_template) }
let(:mustache_template) { "{{ tt.test_class.hello_world }}" }
describe "in the default locale" do
it "returns the translation string" do
_(subject).must_equal "Hello world"
end
end
describe "in the configured locale" do
it "returns the translation string" do
I18n.locale = :de
_(subject).must_equal "Hallo Welt"
end
end
describe "translations with YAML arguments" do
let(:mustache_template) { "{{ tt.test_class.author_info.full }}" }
describe "in the default locale" do
it "autofills YAML arguments" do
_(subject).must_equal "Author J.R.R. is from Bloemfontein"
end
end
describe "in the configured locale" do
it "autofills YAML arguments" do
I18n.locale = :de
_(subject).must_equal "Autor J.R.R. ist vom Bloemfontein"
end
end
end
describe "translations with invalid arguments" do
let(:mustache_template) { "{{ tt.test_class.has_invalid_argument }}" }
it "fails gracefully with embedded error message" do
expected_string = "Welcome to " \
"[#{TestClass::NO_METHOD_MESSAGE}: no_matching_method]"
_(subject).must_equal expected_string
end
end
describe "missing translations" do
let(:mustache_template) {
"{{ tt.test_class.nested.nonexistent_key }}"
}
it "outputs an empty string" do
_(subject).must_be_empty
end
end
end
describe "usage" do
let(:subject) { dummy_instance.tt }
it "returns a hash" do
_(subject).must_be_kind_of Hash
end
it "returns all present translation keys" do
i18n_keys = I18n.t(".").keys
_(subject.keys).must_equal i18n_keys
end
it "returns nested keys" do
nested_keys = subject[:test_class][:author_info].keys
_(nested_keys).must_equal [:full]
end
describe "auto-filled YAML arguments" do
let(:subject) { dummy_instance.tt[:test_class][:author_info][:full] }
it "auto-fills in the default locale" do
_(subject).must_equal "Author J.R.R. is from Bloemfontein"
end
it "auto-fills in a configured locale" do
I18n.locale = :de
_(subject).must_equal "Autor J.R.R. ist vom Bloemfontein"
end
end
end
end
end