Skip to content
Permalink
Browse files

Add a dialog for product translations

  • Loading branch information...
imobachgs committed Feb 12, 2018
1 parent ab70e0d commit 68614fd95dbdc68339068f8b33a65abc1fe46218
@@ -0,0 +1,106 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require "yast"
require "cwm"
require "cwm/dialog"
require "y2packager/widgets/product_license_translations"

Yast.import "Label"

module Y2Packager
module Dialogs
# This dialog displays available translation for a given product.
#
# The dialog is open as a pop-up (check #should_open_dialog,
# #wizard_create_dialog and #layout for technical details) and it relies
# heavily on the {Y2Packager::Widgets::ProductLicenseTranslations} widget.
class ProductLicenseTranslations < CWM::Dialog
# @return [Y2Packager::Product] Product
attr_reader :product
# @return [String] Default language code (eg. "en_US")
attr_reader :language

def initialize(product, language = nil)
super()
@product = product
@language = language || Yast::Language.language
end

# Returns the dialog title
#
# @return [String] Dialog's title
def title
_("License Agreement")
end

# Dialog content
#
# @return [Yast::Term] Dialog's content
def contents
VBox(
Y2Packager::Widgets::ProductLicenseTranslations.new(product, language)
)
end

private

# Force the dialog to be shown as a pop-up
#
# @return [True]
def should_open_dialog?
true
end

# Redefine how the dialog should be created
#
# @see #layout
def wizard_create_dialog(&block)
Yast::UI.OpenDialog(layout)
block.call
ensure
Yast::UI.CloseDialog()
end

# Define widget's layout
#
# @return [Yast::Term]
def layout
HBox(
VSpacing(Yast::UI.TextMode ? 21 : 25),
VBox(
Left(
# TRANSLATORS: dialog caption
Heading(Id(:title), _("License Agreement"))
),
VSpacing(Yast::UI.TextMode ? 0.1 : 0.5),
HSpacing(82),
HBox(
VStretch(),
ReplacePoint(Id(:contents), Empty())
),
ButtonBox(
PushButton(Id(:next), Opt(:okButton, :default, :key_F10), next_button)
)
)
)
end

# Define next button (ok) label
#
# @return [String]
def next_button
Yast::Label.OKButton
end
end
end
end
@@ -12,18 +12,20 @@

require "yast"
require "cwm"
require "y2packager/dialogs/product_license"
require "y2packager/dialogs/product_license_translations"

module Y2Packager
module Widgets
# This button launches the licenses translations dialog when pushed
class LicenseTranslationsButton < CWM::PushButton
# @return [Y2Packager::Product] Product
attr_reader :product
attr_reader :language

def initialize(product)
def initialize(product, language = nil)
super()
@product = product
@language = language || Yast::Language.language
end

# Widget label
@@ -38,7 +40,7 @@ def label
#
# @see CWM::AbstractWidget#handle
def handle
Y2Packager::Dialogs::ProductLicense.new(product).run
Y2Packager::Dialogs::ProductLicenseTranslations.new(product, language).run
nil
end
end
@@ -0,0 +1,83 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require "yast"
require "cwm"
require "y2country/widgets/simple_language_selection"
require "y2packager/widgets/product_license"

module Y2Packager
module Widgets
# This widget display license translations for a given product
#
# The widget serves a glue between a pair of widgets:
#
# * {Y2Country::Widgets::SimpleLanguageSelector} to select the language,
# * {Y2Packager::Widgets::ProductLicenseContent} to display the license.
class ProductLicenseTranslations < CWM::CustomWidget
# @return [Y2Packager::Product] Product
attr_reader :product
# @return [String] Language code (en_US, es_ES, etc.).
attr_reader :language

# @param product [Y2Packager::Product] Product
# @param language [String] Default language (en_US, es_ES, etc.).
def initialize(product, language)
super()
@product = product
@language = language
self.handle_all_events = true
end

# Widget content
#
# @see CWM::CustomWidget#contents
def contents
VBox(
Left(language_selection),
VSpacing(0.5),
product_license
)
end

# Event handler
#
# Translate the license content if language has changed.
#
# @param event [Hash] Event data
def handle(event)
if event["ID"] == language_selection.widget_id
product_license.translate(language_selection.value)
end
nil
end

private

# Language selection widget
#
# @return [Y2Country::Widgets::SimpleLanguageSelection]
def language_selection
@language_selection ||=
Y2Country::Widgets::SimpleLanguageSelection.new(product.license_locales, language)
end

# Product selection widget
#
# @return [Widgets::ProductLicenseContent]
def product_license
@product_license ||=
Y2Packager::Widgets::ProductLicenseContent.new(product, language)
end
end
end
end
@@ -0,0 +1,50 @@
#!/usr/bin/env rspec
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require_relative "../../test_helper"
require "y2packager/dialogs/product_license_translations"
require "y2packager/product"

describe Y2Packager::Dialogs::ProductLicenseTranslations do
subject(:dialog) { described_class.new(product, language) }
let(:product) { instance_double("Y2Packager::Product") }
let(:language) { "en_US" }

describe "#contents" do
it "includes a ProductLicenseTranslations widget" do
expect(Y2Packager::Widgets::ProductLicenseTranslations).to receive(:new)
.with(product, language).and_call_original
expect(dialog.contents.to_s).to include("Widgets::ProductLicenseTranslations")
end
end

describe "#language" do
context "when it is not specified" do
before do
allow(Yast::Language).to receive(:language).and_return("de_DE")
end

it "uses the system's current language" do
dialog = described_class.new(product)
expect(dialog.language).to eq("de_DE")
end
end

context "when it is specified" do
it "uses the given one" do
dialog = described_class.new(product, "cs_CZ")
expect(dialog.language).to eq("cs_CZ")
end
end
end
end
@@ -19,16 +19,20 @@
describe Y2Packager::Widgets::LicenseTranslationsButton do
subject(:widget) { described_class.new(product) }
let(:product) { instance_double(Y2Packager::Product, license: "content") }
let(:language) { "en_US" }

describe "#handle" do
let(:dialog) { instance_double(Y2Packager::Dialogs::ProductLicense, run: nil) }
let(:dialog) { instance_double(Y2Packager::Dialogs::ProductLicenseTranslations, run: nil) }

before do
allow(Y2Packager::Dialogs::ProductLicense).to receive(:new)
.with(product).and_return(dialog)
allow(Yast::Language).to receive(:language).and_return(language)
allow(Y2Packager::Dialogs::ProductLicenseTranslations).to receive(:new)
.and_return(dialog)
end

it "opens a dialog" do
expect(Y2Packager::Dialogs::ProductLicenseTranslations).to receive(:new)
.with(product, language).and_return(dialog)
expect(dialog).to receive(:run)
widget.handle
end
@@ -0,0 +1,79 @@
#!/usr/bin/env rspec
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require_relative "../../test_helper"
require "cwm/rspec"

require "y2packager/widgets/product_license_translations"
require "y2packager/product"

describe Y2Packager::Widgets::ProductLicenseTranslations do
include_examples "CWM::CustomWidget"

subject(:widget) { described_class.new(product, language) }

let(:language) { "de_DE" }
let(:product) do
instance_double(Y2Packager::Product, license_locales: ["en_US"], license: "content")
end

describe "#contents" do
it "includes a language selector" do
expect(Y2Country::Widgets::SimpleLanguageSelection).to receive(:new)
.with(languages: product.license_locales, default: language)
widget.contents
end

it "includes a the product license text" do
expect(Y2Packager::Widgets::ProductLicenseContent).to receive(:new)
.with(product, language)
widget.contents
end
end

describe "#handle" do
let(:language_widget) do
Y2Country::Widgets::SimpleLanguageSelection.new(["en_US", "es"], "en_US")
end
let(:content_widget) { instance_double(Y2Packager::Widgets::ProductLicenseContent) }

before do
allow(Y2Country::Widgets::SimpleLanguageSelection).to receive(:new)
.and_return(language_widget)
allow(Y2Packager::Widgets::ProductLicenseContent).to receive(:new)
.and_return(content_widget)
end

context "when the event comes from the language selector" do
let(:event) { { "ID" => language_widget.widget_id } }

before do
allow(language_widget).to receive(:value).and_return("es")
end

it "translates the license content" do
expect(content_widget).to receive(:translate).with("es")
widget.handle(event)
end
end

context "when the event comes from another widget" do
let(:event) { { "ID" => "other_widget" } }

it "does not translate the license content" do
expect(content_widget).to_not receive(:translate)
widget.handle(event)
end
end
end
end

0 comments on commit 68614fd

Please sign in to comment.
You can’t perform that action at this time.