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
- Fix some locale keys being out of alphabetical order

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
  • Loading branch information
KludgeKML committed Sep 19, 2024
1 parent 41a3f2d commit 7b4e68f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
30 changes: 30 additions & 0 deletions app/helpers/locale_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,34 @@ module LocaleHelper
def lang_attribute(locale)
"lang=#{locale}" unless I18n.default_locale.to_s == locale.to_s
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

def page_text_direction
I18n.t("i18n.direction", locale: I18n.locale, default: "ltr")
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 @@ -17,6 +17,12 @@ def initialize(content_store_response)
delegate :to_h, to: :content_store_response
delegate :cache_control, to: :content_store_response

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 %>
22 changes: 18 additions & 4 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ cy:
components:
figure:
image_credit: 'Delwedd gan: %{credit}'
content_item:
schema_name:
take_part:
few:
many:
one: Cymryd rhan
other: Cymryd rhan
two:
zero:
continue: Parhau
cookies:
always_on: Mae angen iddynt fod ymlaen bob amser.
Expand Down Expand Up @@ -812,6 +821,11 @@ cy:
uk_bank_holidays:
universal_credit:
most_active:
i18n:
direction: ltr
language_names:
cy: Cymraeg
en:
'no': Na
or:
place:
Expand Down Expand Up @@ -874,8 +888,8 @@ cy:
description:
title:
body:
start_title:
start_notes:
end_title:
end_notes:
start_title:
start_notes:
end_title:
end_notes:
'yes': Ie
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ en:
components:
figure:
image_credit: 'Image credit: %{credit}'
content_item:
schema_name:
take_part:
one: Take part
other: Take part
continue: Continue
cookies:
always_on: They always need to be on.
Expand Down Expand Up @@ -650,6 +655,11 @@ en:
link: /vehicle-tax
- title: 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 7b4e68f

Please sign in to comment.