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
  • Loading branch information
KludgeKML committed Sep 5, 2024
1 parent a9b1350 commit ba93080
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 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 @@ -14,4 +14,10 @@ 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
end
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 %>
12 changes: 12 additions & 0 deletions config/locales/cy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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,9 @@ cy:
universal_credit:
most_active:
'no': Na
language_names:
cy: Cymraeg
en:
or:
place:
children_social_care: Gallwch alw'r tîm gofal cymdeithasol i blant yn y cyngor yn
Expand Down
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,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 @@ -651,6 +656,9 @@ en:
link: /vat-rates
'no': 'No'
or: or
language_names:
cy: Welsh
en: English
place:
children_social_care: You can call the children's social care team at the council in
go_to_website: Go to their website
Expand Down

0 comments on commit ba93080

Please sign in to comment.