Skip to content

Commit

Permalink
Add a simple language selection widget
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Feb 15, 2018
1 parent f308a26 commit 12bb5a7
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/lib/y2packager/widgets/product_license_translations.rb
Expand Up @@ -12,7 +12,7 @@

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

module Y2Packager
Expand All @@ -21,7 +21,7 @@ module Widgets
#
# The widget serves a glue between a pair of widgets:
#
# * {Y2Country::Widgets::SimpleLanguageSelector} to select the language,
# * {Y2Packager::Widgets::SimpleLanguageSelector} to select the language,
# * {Y2Packager::Widgets::ProductLicenseContent} to display the license.
class ProductLicenseTranslations < CWM::CustomWidget
# @return [Y2Packager::Product] Product
Expand Down Expand Up @@ -65,10 +65,10 @@ def handle(event)

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

# Product selection widget
Expand Down
113 changes: 113 additions & 0 deletions src/lib/y2packager/widgets/simple_language_selection.rb
@@ -0,0 +1,113 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany.
#
#
# 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.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may find
# current contact information at www.novell.com.
# ------------------------------------------------------------------------------

require "yast"
require "cwm/widget"

Yast.import "Language"

module Y2Packager
module Widgets
# Language selection widget
#
# In contrast to {Y2Packager::Widgets::LanguageSelection}, this modules does not
# modify the system language in any way.
class SimpleLanguageSelection < CWM::ComboBox
# @return [String] Default language code
attr_reader :default
# @return [String] List of languages to display (en_US, de_DE, etc.)
attr_reader :languages

# @param languages [Array<String>] List of languages to display (en_US, de_DE, etc.)
# @param default [String] Default language code
def initialize(languages, default)
textdomain "y2packager"
@languages = languages
@default = default
self.widget_id = "simple_language_selection"
end

# Widget label
#
# @return [String]
def label
_("&Language")
end

# Widget options
#
# Widget is forced to report immediatelly after value changed.
def opt
opts = [:notify]
opts << :disabled unless items.size > 1
opts
end

# [String] Default license language.
DEFAULT_LICENSE_LANG = "en_US".freeze

# Initialize to the given default language
#
# If the language is not in the list of options, it will try with the
# short code (for instance, "de" for "de_DE"). If it fails again, it
# initial value will be set to "en_US".
def init
languages = items.map(&:first)
new_value =
if languages.include?(default)
default
elsif default.include?("_")
short_code = default.split("_").first
languages.include?(short_code) ? short_code : nil
end

self.value = new_value || DEFAULT_LICENSE_LANG
end

# Widget help text
#
# @return [String]
def help
""
end

# Return the options to be shown in the combobox
#
# @return [Array<Array<String,String>>] Array of languages in form [code, description]
def items
return @items if @items
languages_map = Yast::Language.GetLanguagesMap(false)
@items = languages.each_with_object([]) do |code, langs|
attrs = languages_map.key?(code) ? languages_map[code] : nil
lang, attrs = languages_map.find { |k, _v| k.start_with?(code) } if attrs.nil?

if attrs.nil?
log.warn "Not valid language '#{lang}'"
next
end

log.debug "Using language '#{lang}' instead of '#{code}'" if lang =! code
langs << [code, attrs[4]]
end
@items.uniq!
@items.sort_by! { |l| l[1] }
end
end
end
end
6 changes: 3 additions & 3 deletions test/lib/widgets/product_license_translations_test.rb
Expand Up @@ -29,7 +29,7 @@

describe "#contents" do
it "includes a language selector" do
expect(Y2Country::Widgets::SimpleLanguageSelection).to receive(:new)
expect(Y2Packager::Widgets::SimpleLanguageSelection).to receive(:new)
.with(languages: product.license_locales, default: language)
widget.contents
end
Expand All @@ -43,12 +43,12 @@

describe "#handle" do
let(:language_widget) do
Y2Country::Widgets::SimpleLanguageSelection.new(["en_US", "es"], "en_US")
Y2Packager::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)
allow(Y2Packager::Widgets::SimpleLanguageSelection).to receive(:new)
.and_return(language_widget)
allow(Y2Packager::Widgets::ProductLicenseContent).to receive(:new)
.and_return(content_widget)
Expand Down
89 changes: 89 additions & 0 deletions test/lib/widgets/simple_language_selection_test.rb
@@ -0,0 +1,89 @@
#!/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/widgets/simple_language_selection"
require "cwm/rspec"
require "y2packager/product"

describe Y2Packager::Widgets::SimpleLanguageSelection do
include_examples "CWM::AbstractWidget"

subject(:widget) { described_class.new(["de_DE", "en", "cs"], default) }

let(:default) { "en" }
let(:languages_map) do
{
"de_DE" => ["Deutsch", "Deutsch", ".UTF-8", "@euro", "German"],
"en_US" => ["English (US)", "English (US)", ".UTF-8", "", "English (US)"],
"es_ES" => ["Español", "Espanol", ".UTF-8", "@euro", "Spanish"],
"cs_CZ" => ["Čeština", "Cestina", ".UTF-8", "", "Czech"]
}
end

before do
allow(Yast::Language).to receive(:GetLanguagesMap).with(false)
.and_return(languages_map)
end

describe "#init" do
it "sets the widget's value to the default one" do
expect(widget).to receive(:value=).with(default)
widget.init
end

context "when full language code does not exist" do
let(:default) { "cs_CZ" }

it "tries using the short language code" do
expect(widget).to receive(:value=).with("cs")
widget.init
end

context "and short language code does not exist" do
let(:default) { "es" }

it "tries to the default 'en_US'" do
expect(widget).to receive(:value=).with("en_US")
widget.init
end
end
end
end

describe "#items" do
it "contains only given languages" do
expect(widget.items).to eq([
["cs", "Czech"],
["en", "English (US)"],
["de_DE", "German"]
])
end
end

describe "#opt" do
it "sets the :notify option" do
expect(widget.opt).to eq([:notify])
end

context "when there is only one option" do
let(:languages_map) do
{ "en_US" => ["English (US)", "English (US)", ".UTF-8", "", "English (US)"] }
end

it "sets the :disabled option" do
expect(widget.opt).to include(:disabled)
end
end
end
end

0 comments on commit 12bb5a7

Please sign in to comment.