Skip to content

Commit

Permalink
Add translation shared partial
Browse files Browse the repository at this point in the history
- Add methods to local helper 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
  • Loading branch information
KludgeKML committed Oct 31, 2024
1 parent a204d7d commit 8b25bea
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 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 @@ -43,6 +43,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 %>

0 comments on commit 8b25bea

Please sign in to comment.