Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add localized links for links to docs

Oliver Günther426ce50312e8
diff --git a/lib/open_project/static/links.rb b/lib/open_project/static/links.rb
index 958b01a8e91..8c4e7cdf9c6 100644
--- a/lib/open_project/static/links.rb
+++ b/lib/open_project/static/links.rb
@@ -47,13 +47,33 @@ module OpenProject
end
def url_for(*items)
- links.dig(*items, :href)
+ href = links.dig(*items, :href)
+
+ if docs_url?(href)
+ with_locale_param(href)
+ else
+ href
+ end
end
def has?(name)
@links.key? name
end
+ def docs_url?(url)
+ url.start_with?(docs_url)
+ end
+
+ def docs_url
+ links[:openproject_docs][:href]
+ end
+
+ def with_locale_param(href)
+ url = Addressable::URI.parse(href)
+ url.query_values = (url.query_values || {}).merge(go_to_locale: I18n.locale)
+ url.to_s
+ end
+
private
def dynamic_links
diff --git a/lib_static/redmine/i18n.rb b/lib_static/redmine/i18n.rb
index 6291495e0b8..f626818aba9 100644
--- a/lib_static/redmine/i18n.rb
+++ b/lib_static/redmine/i18n.rb
@@ -111,10 +111,10 @@ module Redmine
#
# @param i18n_key [String] The I18n key to translate.
# @param links [Hash] Link names mapped to URLs.
- # @param external [Boolean] Whether the links should be opened as external links, i.e. in a new tab (default: false)
+ # @param external [Boolean] Whether the links should be opened as external links, i.e. in a new tab (default: true)
# @param underline [Boolean] Whether to underline links inserted into the text (default: true)
- def link_translate(i18n_key, links: {}, locale: ::I18n.locale, external: false, underline: true)
- translation = ::I18n.t(i18n_key.to_s, locale:)
+ def link_translate(i18n_key, links: {}, external: true, underline: true) # rubocop:disable Metrics/AbcSize
+ translation = ::I18n.t(i18n_key.to_s)
result = translation.scan(link_regex).inject(translation) do |t, matches|
link, text, key = matches
link_reference = links[key.to_sym]
diff --git a/spec/lib/open_project/static/links_spec.rb b/spec/lib/open_project/static/links_spec.rb
index b970906e140..6b35e1aedd8 100644
--- a/spec/lib/open_project/static/links_spec.rb
+++ b/spec/lib/open_project/static/links_spec.rb
@@ -27,7 +27,6 @@
#
# See COPYRIGHT and LICENSE files for more details.
#++
-
require "spec_helper"
RSpec.describe OpenProject::Static::Links do
@@ -35,9 +34,105 @@ RSpec.describe OpenProject::Static::Links do
subject { described_class.url_for(*args) }
let(:args) { %i[enterprise_features board_view] }
+ let(:locale_param) { "?go_to_locale=#{I18n.locale}" }
+
+ it "resolves the URL stored in the href with a locale" do
+ expect(subject)
+ .to eq("https://www.openproject.org/docs/user-guide/agile-boards/#{locale_param}#action-boards-enterprise-add-on")
+ end
+
+ context "with german locale" do
+ before do
+ I18n.locale = :de
+ end
+
+ it "resolves the URL stored in the href with a locale" do
+ expect(subject)
+ .to eq("https://www.openproject.org/docs/user-guide/agile-boards/#{locale_param}#action-boards-enterprise-add-on")
+ end
+ end
+
+ context "with docs URLs" do
+ let(:args) { %i[sysadmin_docs oidc] }
+
+ it "adds locale parameter to docs URLs" do
+ expect(subject)
+ .to eq("https://www.openproject.org/docs/system-admin-guide/authentication/openid-providers/#{locale_param}")
+ end
+ end
+
+ context "with non-docs URLs" do
+ let(:args) { %i[website] }
+
+ it "does not add locale parameter to non-docs URLs" do
+ expect(subject).to eq("https://www.openproject.org")
+ expect(subject).not_to include("go_to_locale=")
+ end
+ end
+ end
+
+ describe ".docs_url?" do
+ subject { described_class.docs_url?(url) }
+
+ context "with docs URLs" do
+ let(:url) { "https://www.openproject.org/docs/user-guide/agile-boards/" }
+
+ it "returns true for URLs that start with the docs base URL" do
+ expect(subject).to be true
+ end
+ end
+
+ context "with non-docs URLs" do
+ let(:url) { "https://www.openproject.org/enterprise-edition" }
+
+ it "returns false for URLs that do not start with the docs base URL" do
+ expect(subject).to be false
+ end
+ end
+ end
+
+ describe ".with_locale_param" do
+ subject { described_class.with_locale_param(href) }
+
+ let(:href) { "https://www.openproject.org/docs/system-admin-guide/authentication/openid-providers/" }
+
+ before do
+ allow(I18n).to receive(:locale).and_return(:en)
+ end
+
+ it "adds go_to_locale parameter to the URL" do
+ expect(subject).to include("go_to_locale=en")
+ end
+
+ it "preserves the original URL structure" do
+ expect(subject).to start_with(href)
+ end
+
+ context "with URL that already has query parameters" do
+ let(:href) { "https://www.openproject.org/docs/user-guide/agile-boards/?section=boards" }
+
+ it "adds go_to_locale parameter while preserving existing parameters" do
+ expect(subject).to include("section=boards")
+ expect(subject).to include("go_to_locale=en")
+ end
+ end
+
+ context "with different locale" do
+ before do
+ allow(I18n).to receive(:locale).and_return(:de)
+ end
+
+ it "uses the current I18n locale" do
+ expect(subject).to include("go_to_locale=de")
+ end
+ end
+ end
+
+ describe ".docs_url" do
+ subject { described_class.docs_url }
- it "resolves the URL stored in the href" do
- expect(subject).to eq("https://www.openproject.org/docs/user-guide/agile-boards/#action-boards-enterprise-add-on")
+ it "returns the base docs URL" do
+ expect(subject).to eq("https://www.openproject.org/docs/")
end
end
end
diff --git a/spec/lib/redmine/i18n_spec.rb b/spec/lib/redmine/i18n_spec.rb
index a5effa7d76a..4d31c6c2e61 100644
--- a/spec/lib/redmine/i18n_spec.rb
+++ b/spec/lib/redmine/i18n_spec.rb
@@ -186,7 +186,6 @@ module OpenProject
end
describe "link_translation" do
- let(:locale) { :en }
let(:urls) do
{ url_1: "http://openproject.com/foo", url_2: "/baz" }
end
@@ -194,12 +193,12 @@ module OpenProject
before do
allow(::I18n)
.to receive(:t)
- .with("translation_with_a_link", locale: anything)
+ .with("translation_with_a_link")
.and_return("There is a [link](url_1) in this translation! Maybe even [two](url_2)?")
end
it "allows to insert links into translations" do
- translated = link_translate :translation_with_a_link, links: urls
+ translated = link_translate :translation_with_a_link, links: urls, external: false
expect(translated).to eq(
'There is a <a href="http://openproject.com/foo" data-view-component="true" class="Link Link--underline">link</a>' +
@@ -207,20 +206,6 @@ module OpenProject
)
end
- context "with locale" do
- let(:locale) { :de }
-
- it "uses the passed locale" do
- translated = link_translate(:translation_with_a_link, links: urls, locale:)
-
- expect(translated).to eq(
- 'There is a <a href="http://openproject.com/foo" data-view-component="true" class="Link Link--underline">link</a>' +
- ' in this translation! Maybe even <a href="/baz" data-view-component="true" class="Link Link--underline">two</a>?'
- )
- expect(I18n).to have_received(:t).with(anything, locale:)
- end
- end
-
context "when passing URLs as a list of symbols" do
let(:urls) do
{ url_1: [:a, :b], url_2: [:a, :c] }
@@ -233,7 +218,7 @@ module OpenProject
end
it "resolves the links from static links" do
- translated = link_translate :translation_with_a_link, links: urls
+ translated = link_translate :translation_with_a_link, links: urls, external: false
expect(translated).to eq(
'There is a <a href="https://example.com/a-b" data-view-component="true" class="Link Link--underline">link</a>' +