Skip to content

Commit

Permalink
Use the translated product description if available
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Nov 9, 2023
1 parent 0f420c3 commit 0148d27
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
27 changes: 26 additions & 1 deletion service/lib/agama/dbus/software/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def initialize(backend, logger)

def available_base_products
backend.products.map do |id, data|
[id, data["name"], { "description" => data["description"] }].freeze
[id, data["name"], { "description" => localized_description(data) }].freeze
end
end

Expand Down Expand Up @@ -181,6 +181,31 @@ def register_callbacks
end
end

# find translated product description if available
# @param data [Hash] product configuration from the YAML file
# @return [String,nil] Translated product description (if available)
# or the untranslated description, nil if not found
def localized_description(data)
translations = data["translations"]&.[]("description")
lang = ENV["LANG"] || ""

# no translations or language not set, return untranslated value
return data["description"] if !translations.is_a?(Hash) || lang.empty?

# remove the character encoding if present
lang = lang.split(".").first
# full matching (language + country)
return translations[lang] if translations[lang]

# remove the country part
lang = lang.split("_").first
# partial match (just the language)
return translations[lang] if translations[lang]

# fallback to original untranslated description
data["description"]
end

USER_SELECTED_PATTERN = 0
AUTO_SELECTED_PATTERN = 1
def compute_patterns
Expand Down
2 changes: 1 addition & 1 deletion service/lib/agama/ui_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def initialize(locale_client, &block)
def change_locale(locale)
# TODO: check if we can use UTF-8 everywhere including strange arch consoles
Yast::WFM.SetLanguage(locale, "UTF-8")
# explicitelly set ENV to get localization also from libraries like libstorage
# explicitly set ENV to get localization also from libraries like libstorage
ENV["LANG"] = locale + ".UTF-8"
log.info "set yast language to #{locale}"
# explicit call to textdomain to force fast gettext change of language ASAP
Expand Down
63 changes: 63 additions & 0 deletions service/test/agama/dbus/software/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,67 @@
expect(installed).to eq(true)
end
end

describe "#available_base_products" do
products = {
"Tumbleweed" => {
"id" => "Tumbleweed",
"name" => "openSUSE Tumbleweed",
"description" => "Original description",
"translations" => {
"description" => {
"cs" => "Czech translation",
"es" => "Spanish translation"
}
}
}
}

before do
expect(backend).to receive(:products).and_return(products)
end

it "returns product ID and name" do
product = subject.available_base_products.first
expect(product[0]).to eq("Tumbleweed")
expect(product[1]).to eq("openSUSE Tumbleweed")
end

it "returns untranslated description when the language is not set" do
allow(ENV).to receive(:[]).with("LANG").and_return(nil)

product = subject.available_base_products.first
expect(product[2]["description"]).to eq("Original description")
end

it "returns Czech translation if locale is \"cs_CZ.UTF-8\"" do
allow(ENV).to receive(:[]).with("LANG").and_return("cs_CZ.UTF-8")

product = subject.available_base_products.first
expect(product[2]["description"]).to eq("Czech translation")
end

it "returns Czech translation if locale is \"cs\"" do
allow(ENV).to receive(:[]).with("LANG").and_return("cs")

product = subject.available_base_products.first
expect(product[2]["description"]).to eq("Czech translation")
end

it "return untranslated description when translation is not available" do
allow(ENV).to receive(:[]).with("LANG").and_return("cs_CZ.UTF-8")

products = {
"Tumbleweed" => {
"id" => "Tumbleweed",
"name" => "openSUSE Tumbleweed",
"description" => "Original description"
}
}
expect(backend).to receive(:products).and_return(products)

product = subject.available_base_products.first
expect(product[2]["description"]).to eq("Original description")
end
end
end

0 comments on commit 0148d27

Please sign in to comment.