Skip to content

Commit

Permalink
Merge pull request #2153 from DanielKuhn/ai_translation
Browse files Browse the repository at this point in the history
Create configuration option for specifying a translator url
  • Loading branch information
tf authored Oct 7, 2024
2 parents cd80012 + f6d2969 commit b224d8d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/views/components/pageflow/admin/entry_translations_tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def build(entry)
end

add_link(entry)
generate_link(entry)
end

private
Expand Down Expand Up @@ -84,6 +85,17 @@ def add_link(entry)
new_admin_entry_translation_path(entry),
class: 'button'))
end

def generate_link(entry)
return unless authorized?(:manage_translations, entry)
entry_translator_url = Pageflow.config_for(entry).entry_translator_url
return if entry_translator_url.blank?

text_node(link_to(t('pageflow.admin.entry_translations.generate'),
entry_translator_url.call(entry),
class: 'button',
style: "margin-left: 8px"))
end
end
end
end
1 change: 1 addition & 0 deletions config/locales/new/entry_translations.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ de:
translations: Übersetzungen
entry_translations:
add: Übersetzung zuordnen
generate: Übersetzung generieren
entry_id: Beitrag
id: Übersetzung(en)
new_hint_html: |-
Expand Down
1 change: 1 addition & 0 deletions config/locales/new/entry_translations.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ en:
translations: Translations
entry_translations:
add: Link translation
generate: Generate translation
entry_id: Entry
id: Translation(s)
new_hint_html: |-
Expand Down
11 changes: 11 additions & 0 deletions lib/pageflow/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ class Configuration
# @since 12.2
attr_accessor :news

# URL to a form that is used to translate an entry via an external service.
#
# @example
#
# config.entry_translator_url = lambda do |entry|
# Rails.application.routes.url_helpers.some_custom_route(entry)
# end
# @since edge
attr_accessor :entry_translator_url

def initialize(target_type_name = nil)
@target_type_name = target_type_name

Expand Down Expand Up @@ -618,6 +628,7 @@ def enable_all_features
delegate :public_entry_cache_control_header=, to: :config
delegate :additional_public_entry_headers, to: :config
delegate :cutoff_modes, to: :config
delegate :entry_translator_url=, to: :config

delegate :for_entry_type, to: :config
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ module Pageflow
expect(rendered).to have_selector('a', text: 'Mark as default')
end

it 'renders a link to entry translator url' do
pageflow_configure do |config|
config.entry_translator_url = lambda do |entry|
"http://example.com?entry_id=#{entry.id}"
end
end

de_entry = create(:entry, draft_attributes: {locale: 'de'})

allow(helper).to receive(:authorized?).and_return(true)

render(de_entry)

expect(rendered).to have_selector('a', text: 'Generate translation')
expect(rendered).to have_selector("a[href='http://example.com?entry_id=#{de_entry.id}']")
end

it 'includes self in table without link' do
de_entry = create(:entry, title: 'My story DE', draft_attributes: {locale: 'de'})
en_entry = create(:entry, title: 'My story EN', draft_attributes: {locale: 'en'})
Expand Down Expand Up @@ -67,6 +84,28 @@ module Pageflow
expect(rendered).to have_selector('a', text: 'Mark as default')
end

it 'hides generate link by default' do
de_entry = create(:entry, draft_attributes: {locale: 'de'})

allow(helper).to receive(:authorized?).and_return(true)

render(de_entry)

expect(rendered).not_to have_selector('a', text: 'Generate translation')
end

it 'hides generate link if user cannot manage translations' do
de_entry = create(:entry, draft_attributes: {locale: 'de'})

allow(helper).to receive(:authorized?).and_return(true)
allow(helper)
.to receive(:authorized?).with(:manage_translations, de_entry).and_return(false)

render(de_entry)

expect(rendered).not_to have_selector('a', text: 'Generate translation')
end

it 'hides remove and add links if user cannot manage translations' do
de_entry = create(:entry, draft_attributes: {locale: 'de'})
en_entry = create(:entry, title: 'My story EN', draft_attributes: {locale: 'en'})
Expand Down

0 comments on commit b224d8d

Please sign in to comment.