Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my wild guess here is that it won't raise signal when localization changes. So if UI cache list of base products, it will use it with old locale.

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
64 changes: 64 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,68 @@
expect(installed).to eq(true)
end
end

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

it "returns product ID and name" do
expect(backend).to receive(:products).and_return(products)

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)
expect(backend).to receive(:products).and_return(products)

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")
expect(backend).to receive(:products).and_return(products)

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")
expect(backend).to receive(:products).and_return(products)

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")

# testing product without translations
untranslated = {
"Tumbleweed" => {
"name" => "openSUSE Tumbleweed",
"description" => "Original description"
}
}
expect(backend).to receive(:products).and_return(untranslated)

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