Skip to content

Commit

Permalink
Add translation shared partial
Browse files Browse the repository at this point in the history
- Add helper to provide language names and translate content info
  into component format
- Add methods to ContentItem to provide available translations

Commit audit trail:
- app/helpers/locale_helper.rb alphagov/government-frontend@90f0dd8
  https://github.com/alphagov/government-frontend/blame/517e4d4619595806bc9a1fb904b2badb65332eaf/app/helpers/application_helper.rb
- app/views/shared/_translations.html.erb alphagov/government-frontend@887399c
- en.yml/cy.yml alphagov/government-frontend@665e1c1
  • Loading branch information
KludgeKML committed Oct 30, 2024
1 parent 43a2985 commit 2066ed1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
26 changes: 26 additions & 0 deletions app/helpers/locale_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,30 @@ def lang_attribute(locale)
def page_text_direction
I18n.t("i18n.direction", locale: I18n.locale, default: "ltr")
end

def native_language_name_for(locale)
I18n.t("language_names.#{locale}", locale:)
end

def translations_for_nav(translations)
translations.map do |translation|
{
locale: translation["locale"],
base_path: translation["base_path"],
text: native_language_name_for(translation["locale"]),
}.tap do |h|
h[:active] = true if h[:locale] == I18n.locale.to_s
end
end
end

def t_locale_fallback(key, options = {})
options[:locale] = I18n.locale
options[:fallback] = nil
translation = I18n.t(key, **options)

if translation.nil? || translation.downcase.include?("translation missing")
I18n.default_locale
end
end
end
6 changes: 6 additions & 0 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ def method_missing(method_name, *_args, &_block)
end
end

def available_translations
translations = content_store_response["links"]["available_translations"] || []

translations.sort_by { |t| t["locale"] == I18n.default_locale.to_s ? "" : t["locale"] }
end

private

def ordered_related_items(links)
Expand Down
6 changes: 6 additions & 0 deletions app/views/shared/_translations.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% if @content_item.available_translations.any? %>
<div class="govuk-grid-column-one-third">
<%= render 'govuk_publishing_components/components/translation_nav',
translations: translations_for_nav(@content_item.available_translations) %>
</div>
<% end %>
5 changes: 4 additions & 1 deletion config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,10 @@ cy:
universal_credit:
most_active:
i18n:
direction:
direction: ltr
language_names:
cy: Cymraeg
en:
'no': Na
or:
place:
Expand Down
23 changes: 13 additions & 10 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -639,27 +639,30 @@ en:
universal_credit: Sign in to your Universal Credit account
most_active:
- title: Find a job
link: "/find-a-job"
link: /find-a-job
- title: Log in to student finance
link: "/student-finance-register-login"
link: /student-finance-register-login
- title: Passport fees
link: "/passport-fees"
link: /passport-fees
- title: Jobseeker's Allowance
link: "/jobseekers-allowance"
link: /jobseekers-allowance
- title: Council Tax bands
link: "/council-tax-bands"
link: /council-tax-bands
- title: Running a limited company
link: "/running-a-limited-company"
link: /running-a-limited-company
- title: Driving theory test
link: "/book-theory-test"
link: /book-theory-test
- title: Vehicle tax rates
link: "/calculate-tax-rates-for-new-cars"
link: /calculate-tax-rates-for-new-cars
- title: Renew vehicle tax
link: "/vehicle-tax"
link: /vehicle-tax
- title: VAT rates
link: "/vat-rates"
link: /vat-rates
i18n:
direction: ltr
language_names:
cy: Welsh
en: English
'no': 'No'
or: or
place:
Expand Down

0 comments on commit 2066ed1

Please sign in to comment.