Skip to content

Commit

Permalink
Merge pull request #8964 from alphagov/worlwide-organisation-page-tra…
Browse files Browse the repository at this point in the history
…nslations

Worldwide organisation page translations
  • Loading branch information
brucebolt authored Apr 22, 2024
2 parents 8ec077d + 56cc938 commit a377653
Show file tree
Hide file tree
Showing 22 changed files with 503 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="app-vc-worldwide-pages-index-page-summary-card-component">
<%= render "components/summary_card", {
title: page.title,
title: title,
rows: rows,
summary_card_actions: summary_card_actions,
} %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# frozen_string_literal: true

class Admin::WorldwideOrganisationPages::Index::SummaryCardComponent < ViewComponent::Base
attr_reader :page
attr_reader :page, :worldwide_organisation

def initialize(page:)
def initialize(page:, worldwide_organisation:)
@page = page
@worldwide_organisation = worldwide_organisation
end

private

def title
non_english_translation? ? "#{page.default_locale_title} - #{page.translation_locale.native_and_english_language_name}" : page.title
end

def rows
[
summary_row,
Expand Down Expand Up @@ -37,22 +42,48 @@ def body_row
def summary_card_actions
[
edit_action,
add_translation_action,
confirm_destroy_action,
].compact
end

def edit_action
href = if non_english_translation?
edit_admin_editionable_worldwide_organisation_page_translation_path(worldwide_organisation, page, page.translation_locale)
else
edit_admin_editionable_worldwide_organisation_page_path(page.edition, page)
end

{
label: "Edit",
href: edit_admin_editionable_worldwide_organisation_page_path(page.edition, page),
href:,
}
end

def add_translation_action
return if page.missing_translations.blank? || non_english_translation?

{
label: "Add translation",
href: admin_editionable_worldwide_organisation_page_translations_path(worldwide_organisation, page, page.translation_locale),
}
end

def confirm_destroy_action
href = if non_english_translation?
confirm_destroy_admin_editionable_worldwide_organisation_page_translation_path(worldwide_organisation, page, page.translation_locale)
else
confirm_destroy_admin_editionable_worldwide_organisation_page_path(worldwide_organisation, page)
end

{
label: "Delete",
href: confirm_destroy_admin_editionable_worldwide_organisation_page_path(page.edition, page),
href:,
destructive: true,
}
end

def non_english_translation?
page.translation_locale.code != :en
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Admin::WorldwideOrganisationPageTranslationsController < Admin::BaseController
include TranslationControllerConcern

def index; end

def edit; end

private

def create_redirect_path
edit_admin_editionable_worldwide_organisation_page_translation_path(@worldwide_organisation, @worldwide_page, id: translation_locale)
end

def destroy_redirect_path
admin_editionable_worldwide_organisation_pages_path(@worldwide_organisation)
end

def update_redirect_path
admin_editionable_worldwide_organisation_pages_path(@worldwide_organisation)
end

def load_translatable_item
@worldwide_organisation = Edition.find(params[:editionable_worldwide_organisation_id])
@worldwide_page = @worldwide_organisation.pages.find(params[:page_id])
end

def load_translated_models
@translated_page = LocalisedModel.new(@worldwide_page, translation_locale.code)
@english_page = LocalisedModel.new(@worldwide_page, :en)
end

def translatable_item
@translated_page
end

def translated_item_name
@translated_page.title
end

def translation_params
params.require(:page)
.permit(:title,
:summary,
:body)
end
end
6 changes: 6 additions & 0 deletions app/models/corporate_information_page_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def title(organisation)
I18n.t("corporate_information_page.type.title.#{translation_key}", organisation_name: organisation_name(organisation))
end

def default_locale_title(organisation)
I18n.with_locale(:en) do
title(organisation)
end
end

def title_lang(organisation)
translation_key = slug.tr("-", "_")
t_lang("corporate_information_page.type.title.#{translation_key}", organisation_name: organisation_name(organisation))
Expand Down
4 changes: 2 additions & 2 deletions app/models/editionable_worldwide_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def self.format_name
end

def destroy_associated(locale)
offices.each do |office|
office.contact.remove_translations_for(locale)
[offices.map(&:contact), pages].flatten.each do |association|
association.remove_translations_for(locale)
end
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/worldwide_organisation_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ class WorldwideOrganisationPage < ApplicationRecord
include HasContentId
include Attachable

include TranslatableModel
translates :title, :summary, :body

def title(_locale = :en)
corporate_information_page_type.title(edition)
end

def default_locale_title
corporate_information_page_type.default_locale_title(edition)
end

def corporate_information_page_type
CorporateInformationPageType.find_by_id(corporate_information_page_type_id)
end
Expand All @@ -45,6 +52,10 @@ def self.for_slug!(slug)
end
end

def missing_translations
super & edition.non_english_translated_locales
end

def publicly_visible?
true
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% content_for :context, @worldwide_page.title %>
<% content_for :page_title, "Delete #{@translation_locale.native_and_english_language_name} translation" %>
<% content_for :title, "Delete translation" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
<section class="govuk-grid-column-two-thirds">
<%= form_with url: admin_editionable_worldwide_organisation_page_translation_path(@worldwide_organisation, @worldwide_page, @translation_locale), method: :delete do %>
<p class="govuk-body govuk-!-margin-bottom-7">
Are you sure you want to delete the "<%= @translation_locale.native_and_english_language_name %>" translation for "<%= @worldwide_page.title %>"?
</p>

<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Delete",
destructive: true,
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "worldwide-page-translations-button",
"track-label": "Delete",
},
} %>
<%= link_to("Cancel", admin_editionable_worldwide_organisation_pages_path(@worldwide_organisation), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</section>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<% content_for :page_title, "#{@translated_page.translation.persisted? ? "Edit" : "New"} translation for #{@worldwide_page.title}" %>
<% content_for :title, "#{@translated_page.translation.persisted? ? "Edit" : "New"} translation" %>
<% content_for :context, @worldwide_page.title %>
<% content_for :error_summary, render(Admin::ErrorSummaryComponent.new(object: @translated_page)) %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with model: @worldwide_page, as: :contact, url: admin_editionable_worldwide_organisation_page_translation_path(@worldwide_organisation, @worldwide_page, translation_locale), method: :put do |form| %>
<%= render "components/translated_textarea", {
textarea: {
label: {
heading_size: "l",
text: "Translated summary",
},
name: "page[summary]",
id: "page_summary",
value: @translated_page.summary,
rows: 2,
error_items: errors_for(form.object.errors, :summary),
right_to_left: @translated_page.translation_locale.rtl?,
right_to_left_help: false,
},
details: {
text: @worldwide_page.summary,
},
} %>
<%= render "components/translated_textarea", {
textarea: {
label: {
heading_size: "l",
text: "Translated body (required)",
},
name: "page[body]",
id: "page_body",
value: @translated_page.body,
rows: 20,
error_items: errors_for(form.object.errors, :body),
right_to_left: @translated_page.translation_locale.rtl?,
right_to_left_help: false,
},
details: {
text: @worldwide_page.body,
},
} %>

<div class="govuk-button-group govuk-!-margin-top-8">
<%= render "govuk_publishing_components/components/button", {
text: "Save",
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "worldwide-office-translation-button",
"track-label": "Save",
},
} %>
<% if @translated_page.translation.persisted? %>
<%= link_to("Cancel", admin_editionable_worldwide_organisation_pages_path(@worldwide_organisation), class: "govuk-link govuk-link--no-visited-state") %>
<% else %>
<%= link_to("Cancel", admin_editionable_worldwide_organisation_page_translations_path(@worldwide_organisation, @worldwide_page), class: "govuk-link govuk-link--no-visited-state") %>
<% end %>
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<% content_for :page_title, "New translation for #{@worldwide_page.title}" %>
<% content_for :title, "New translation" %>
<% content_for :context, @worldwide_page.title %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with url: admin_editionable_worldwide_organisation_page_translations_path(@worldwide_organisation, @worldwide_page) do %>
<%= render "govuk_publishing_components/components/select", {
id: "translation_locale",
name: "translation_locale",
label: "Select language",
hint: "This is the first step before creating a translated version of this content",
heading_size: "l",
full_width: true,
options: @worldwide_page.missing_translations.map do |locale|
{
value: locale.code,
text: Locale.coerce(locale).native_and_english_language_name,
}
end,
} %>

<div class="govuk-button-group govuk-!-margin-top-8">
<%= render "govuk_publishing_components/components/button", {
text: "Next",
data_attributes: {
module: "gem-track-click",
"track-category": "form-button",
"track-action": "worldwide-office-translation-button",
"track-label": "Next",
},
} %>
<%= link_to("Cancel", admin_editionable_worldwide_organisation_pages_path(@worldwide_organisation), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
17 changes: 16 additions & 1 deletion app/views/admin/worldwide_organisation_pages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@
<div class="govuk-grid-column-two-thirds">
<% if @worldwide_organisation.pages.any? %>
<% @worldwide_organisation.pages.each do |page| %>
<%= render Admin::WorldwideOrganisationPages::Index::SummaryCardComponent.new(page:) %>
<%= render Admin::WorldwideOrganisationPages::Index::SummaryCardComponent.new(page:, worldwide_organisation: @worldwide_organisation) %>
<% end %>
<% if @worldwide_organisation.pages.any? { |page| page.non_english_localised_models.present? } %>
<%= render "govuk_publishing_components/components/heading", {
text: "Translated",
font_size: "m",
margin_bottom: 6,
} %>
<% @worldwide_organisation.pages.each do |page| %>
<% page.non_english_localised_models.each do |translation| %>
<%= render Admin::WorldwideOrganisationPages::Index::SummaryCardComponent.new(page: translation, worldwide_organisation: @worldwide_organisation) %>
<% end %>
<% end %>
<% end %>
<% else %>
<%= render "govuk_publishing_components/components/inset_text", {
text: "No pages.",
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
resources :editionable_worldwide_organisations, path: "editionable-worldwide-organisations", except: [:index] do
resources :pages, controller: "worldwide_organisation_pages" do
get :confirm_destroy, on: :member

resources :translations, controller: "worldwide_organisation_page_translations", only: %i[create edit update destroy index] do
get :confirm_destroy, on: :member
end
end
end
resources :worldwide_organisation_pages, only: [] do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddWorldwideOrganisationPageTranslationsTable < ActiveRecord::Migration[7.1]
def up
WorldwideOrganisationPage.create_translation_table! title: :string, summary: :text, body: :text
end

def down
WorldwideOrganisationPage.drop_translation_table!
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveWorldwideOrganisationPageFields < ActiveRecord::Migration[7.1]
def change
remove_columns(:worldwide_organisation_pages, :body, :summary, type: "text")
end
end
Loading

0 comments on commit a377653

Please sign in to comment.