diff --git a/lib/gollum/locales/en.yml b/lib/gollum/locales/en.yml new file mode 100644 index 00000000..fafc5dc5 --- /dev/null +++ b/lib/gollum/locales/en.yml @@ -0,0 +1,38 @@ +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 + comparison_of: Comparison of + comparing_versions_of: Comparing versions of + comparing_from: "Comparing %{before} to %{after}" + revert: Revert Changes + precious/views/error: + error: Error + precious/views/history: + browse_in_history_description: Browse the page at this point in the history + compare_revisions: Compare Revisions + history_for: History for + precious/views/latest_changes: + title: Latest Changes (Globally) + precious/views/layout: + title: Home + precious/views/overview: + back_to_top: Back to Top + delete_confirmation: "Are you sure you want to delete %{name}?" + no_pages_in: There are no pages in + on: "on" + title: "Overview of %{ref}" + precious/views/search: + aria: + show_all: "Show all %{count} hits in this page" + back_to_top: Back to Top + no_results: There are no results for your search + search_results_for: Search results for + title: "Search results for %{query}" diff --git a/lib/gollum/templates/compare.mustache b/lib/gollum/templates/compare.mustache index b26e84e7..dd80b72e 100644 --- a/lib/gollum/templates/compare.mustache +++ b/lib/gollum/templates/compare.mustache @@ -4,55 +4,84 @@
{{message}}
+{{message}}
{{/message}} -| {{ldln}} | -{{rdln}} | -
- {{line}}
- |
-
| {{ldln}} | +{{rdln}} | +
+ {{line}}
+ |
+
{{message}}
- There are no pages in {{current_path}} on {{ref}}. + {{t.no_pages_in}} + {{current_path}} + {{t.on}} + {{ref}}.
{{/no_results}} diff --git a/lib/gollum/templates/pagination.mustache b/lib/gollum/templates/pagination.mustache index a100f97f..020e0976 100644 --- a/lib/gollum/templates/pagination.mustache +++ b/lib/gollum/templates/pagination.mustache @@ -1,6 +1,23 @@ - diff --git a/lib/gollum/templates/search.mustache b/lib/gollum/templates/search.mustache index 365a6f17..b8b38b41 100644 --- a/lib/gollum/templates/search.mustache +++ b/lib/gollum/templates/search.mustache @@ -4,7 +4,7 @@- There are no results for your search {{query}}. + {{t.no_results}} {{query}}.
{{/no_results}} diff --git a/lib/gollum/views/compare.rb b/lib/gollum/views/compare.rb index 83de12fd..ea4ee88a 100644 --- a/lib/gollum/views/compare.rb +++ b/lib/gollum/views/compare.rb @@ -6,7 +6,7 @@ module Precious attr_reader :page, :diff, :versions, :message, :allow_editing def title - "Comparison of #{@page.title}" + [t[:comparison_of], @page.title].join(" ") end def before diff --git a/lib/gollum/views/helpers/locale_helpers.rb b/lib/gollum/views/helpers/locale_helpers.rb index da15bc76..86e25333 100644 --- a/lib/gollum/views/helpers/locale_helpers.rb +++ b/lib/gollum/views/helpers/locale_helpers.rb @@ -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 diff --git a/lib/gollum/views/latest_changes.rb b/lib/gollum/views/latest_changes.rb index 271391b6..70fe0ed5 100644 --- a/lib/gollum/views/latest_changes.rb +++ b/lib/gollum/views/latest_changes.rb @@ -9,7 +9,7 @@ module Precious attr_reader :wiki def title - "Latest Changes (Globally)" + t[:title] end def versions diff --git a/lib/gollum/views/layout.rb b/lib/gollum/views/layout.rb index 0061b647..a30773e7 100644 --- a/lib/gollum/views/layout.rb +++ b/lib/gollum/views/layout.rb @@ -20,7 +20,7 @@ module Precious end def title - "Home" + t[:title] end def has_path @@ -58,11 +58,11 @@ module Precious def js # custom js @js end - + def critic_markup @critic_markup end - + def per_page_uploads @per_page_uploads end @@ -75,19 +75,19 @@ module Precious def search false end - + def history false end - + def overview false end - + def latest_changes false end - + end end end diff --git a/lib/gollum/views/overview.rb b/lib/gollum/views/overview.rb index 0c651296..d0e798a4 100644 --- a/lib/gollum/views/overview.rb +++ b/lib/gollum/views/overview.rb @@ -3,11 +3,11 @@ require 'pathname' module Precious module Views class Overview < Layout - attr_reader :results, :ref, :allow_editing, :newable + attr_reader :name, :results, :ref, :allow_editing, :newable HIDDEN_PATHS = ['.gitkeep'] def title - "Overview of #{@ref}" + t[:title] end # def editable @@ -38,21 +38,21 @@ module Precious end end - + def files_folders if has_results files_and_folders = [] - + @results.each do |result| result_path = result.url_path - result_path = result_path.sub(/^#{Regexp.escape(@path)}\//, '') unless @path.nil? + result_path = result_path.sub(/^#{Regexp.escape(@path)}\//, '') unless @path.nil? if result_path.include?('/') # result contains a folder folder_name = result_path.split('/').first folder_path = @path ? "#{@path}/#{folder_name}" : folder_name folder_url = "#{overview_path}/#{folder_path}/" files_and_folders << {name: folder_name, icon: rocticon('file-directory'), type: 'dir', url: folder_url, is_file: false} - elsif !HIDDEN_PATHS.include?(result_path) + elsif !HIDDEN_PATHS.include?(result_path) file_url = page_route(result.escaped_url_path) files_and_folders << {name: result.filename, icon: rocticon('file'), type: 'file', url: file_url, file_path: result.escaped_url_path, is_file: true} end @@ -61,7 +61,7 @@ module Precious files_and_folders.uniq{|f| f[:name]}.sort_by!{|f| [f[:type], f[:name]]} end end - + def has_results !@results.empty? @@ -70,12 +70,12 @@ module Precious def no_results @results.empty? end - + def latest_changes true end - - + + end end end diff --git a/lib/gollum/views/pagination.rb b/lib/gollum/views/pagination.rb index c7b50720..68207ec7 100644 --- a/lib/gollum/views/pagination.rb +++ b/lib/gollum/views/pagination.rb @@ -10,4 +10,4 @@ module Precious @page_num == 1 ? nil : (@page_num - 1).to_s end end -end \ No newline at end of file +end diff --git a/lib/gollum/views/search.rb b/lib/gollum/views/search.rb index 05da778f..4ace9e3e 100644 --- a/lib/gollum/views/search.rb +++ b/lib/gollum/views/search.rb @@ -23,7 +23,7 @@ module Precious end def title - "Search results for " + @query + t[:title] end def search diff --git a/test/gollum/views/test_locale_helper.rb b/test/gollum/views/test_locale_helper.rb index b82f8607..18ce4be7 100644 --- a/test/gollum/views/test_locale_helper.rb +++ b/test/gollum/views/test_locale_helper.rb @@ -15,12 +15,13 @@ describe Precious::Views::LocaleHelpers do end def setup - ::I18n.available_locales = [:en, :de] - ::I18n.load_path = Dir[File.expand_path("test/support/locales" + "/*.yml")] + I18n.available_locales = [:en, :de] + I18n.load_path = Dir[File.expand_path("test/support/locales" + "/*.yml")] end def teardown I18n.locale = :en + I18n.load_path = Dir[::File.expand_path("lib/gollum/locales") + "/*.yml"] end let(:dummy_instance) { TestClass.new } @@ -131,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