Skip to content

Commit

Permalink
Merge pull request #139 from yast/language_selection_for_caasp
Browse files Browse the repository at this point in the history
Language selection for caasp
  • Loading branch information
jreidinger committed Sep 7, 2017
2 parents 7db188f + 825c1e7 commit ae6df11
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 15 deletions.
11 changes: 7 additions & 4 deletions keyboard/src/lib/y2country/widgets.rb
Expand Up @@ -23,14 +23,16 @@
require "cwm/widget"

Yast.import "Keyboard"
Yast.import "Language"

module Y2Country
module Widgets
# Common parts for {KeyboardSelection} and {KeyboardSelectionCombo}.
module KeyboardSelectionBase
# param default [String] ID for default keyboard layout if not selected.
# Allowed values are defined in /usr/share/YaST2/data/keyboard_raw.ycp
def initialize(default)
# param default [String] ID for default keyboard layout if not selected
# and no proposal for current language is found.
# Allowed values are defined in /usr/share/YaST2/data/keyboard_raw.ycp
def initialize(default = "english-us")
textdomain "country"
@default = default
end
Expand All @@ -49,7 +51,8 @@ def init
if Yast::Keyboard.user_decision
self.value = Yast::Keyboard.current_kbd
else
self.value = @default
initial_value = Yast::Keyboard.GetKeyboardForLanguage(Yast::Language.language, @default)
self.value = initial_value
Yast::Keyboard.Set(value)
end
end
Expand Down
10 changes: 8 additions & 2 deletions language/src/Makefile.am
Expand Up @@ -88,6 +88,12 @@ ydata_DATA = \
desktop_DATA = \
desktop/yast-language.desktop

EXTRA_DIST = $(module_DATA) $(module1_DATA) $(client_DATA) $(scrconf_DATA) $(schemafiles_DATA) $(ydata_DATA) $(desktop_DATA)

include $(top_srcdir)/Makefile.am.common
ywidgetsdir = @ylibdir@/y2country/widgets
ywidgets_DATA = \
lib/y2country/widgets/language_selection.rb

EXTRA_DIST = $(module_DATA) $(module1_DATA) $(client_DATA) $(scrconf_DATA) \
$(schemafiles_DATA) $(ydata_DATA) $(desktop_DATA) $(ywidgets_DATA)

include $(top_srcdir)/Makefile.am.common
127 changes: 127 additions & 0 deletions language/src/lib/y2country/widgets/language_selection.rb
@@ -0,0 +1,127 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2017 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 "Console"
Yast.import "Language"
Yast.import "Timezone"
Yast.import "UI"
Yast.import "Mode"

module Y2Country
module Widgets
# Language selection widget
class LanguageSelection < CWM::ComboBox

attr_reader :default

# @param emit_event [Boolean] flag if handle of widget emit `:redraw` event
# when language changed or not
def initialize(emit_event: false)
textdomain "country"
@default = Yast::Language.language
@emit_event = emit_event
end

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

# Widget options
#
# Widget is forced to report immediatelly after value changed.
def opt
[:notify, :hstretch]
end

# Initialize the selected value
def init
self.value = default
end

# Widget help text
#
# @return [String]
def help
_(
"<p>\n" \
"Choose the <b>Language</b> to be used during\n" \
"installation and on the installed system.\n" \
"</p>\n"
)
end

# Handle value changes
def handle
return if value.nil? || value == default
Yast::Timezone.ResetZonemap
Yast::Language.Set(value)
Yast::Language.languages = Yast::Language.RemoveSuffix(value)
@default = value
return nil if !@emit_event || Yast::Mode.config

switch_language
:redraw
end

# Store widget value
def store
handle

switch_language if !@emit_event && !Yast::Mode.config

nil
end

# Return the options to be shown in the combobox
#
# @return [Array<Array<String,String>>] Array of languages in form [code, description]
def items
@items ||= Yast::Language.GetLanguageItems(:first_screen).map do |item|
id, description = item.to_a
code = id.first
[code, description]
end
end

private

def switch_language
if Yast::Language.SwitchToEnglishIfNeeded(true)
log.debug "UI switched to en_US"
else
Yast::Console.SelectFont(Yast::Language.language)
# no yast translation for nn_NO, use nb_NO as a backup
# FIXME: remove the hack, please
if Yast::Language.language == "nn_NO"
log.info "Nynorsk not translated, using Bokm\u00E5l"
Yast::Language.WfmSetGivenLanguage("nb_NO")
else
Yast::Language.WfmSetLanguage
end
end
end
end
end
end
5 changes: 3 additions & 2 deletions language/test/Makefile.am
Expand Up @@ -3,10 +3,11 @@
#

TESTS = \
Language_test.rb
Language_test.rb \
widgets/language_selection_test.rb

TEST_EXTENSIONS = .rb
RB_LOG_COMPILER = rspec
RB_LOG_COMPILER = rspec --format doc
VERBOSE = 1
EXTRA_DIST = $(TESTS)

6 changes: 1 addition & 5 deletions language/test/test_helper.rb
@@ -1,5 +1 @@
root_location = File.expand_path("../../", __FILE__)
ENV["Y2DIR"] = File.expand_path("../../src", __FILE__)

require "yast"
require "yast/rspec"
require_relative "../../test/test_helper"
131 changes: 131 additions & 0 deletions language/test/widgets/language_selection_test.rb
@@ -0,0 +1,131 @@
#!/usr/bin/env rspec --format doc

require_relative "../test_helper"
require "y2country/widgets/language_selection"

describe Y2Country::Widgets::LanguageSelection do

subject(:widget) { described_class.new }
let(:default_language) { "en_US" }

LANGUAGES = [["af_ZA", "Afrikaans - Afrikaans"], ["en_US", "English (US)"]].freeze
LANGUAGE_ITEMS = LANGUAGES.map do |lang|
code, description = lang
Yast::Term.new(:item, Yast::Term.new(:id, code), description)
end.freeze

before do
allow(Yast::Language).to receive(:GetLanguageItems)
allow(Yast::Language).to receive(:GetLanguageItems)
.with(:first_screen).and_return(LANGUAGE_ITEMS)
end

shared_examples "switch language" do |method|
before do
allow(Yast::Language).to receive(:SwitchToEnglishIfNeeded).and_return(false)
allow(Yast::Console).to receive(:SelectFont)
allow(Yast::Language).to receive(:language).and_return("cs_CZ")
# value have to be different otherwise it is skipped
allow(Yast::Language).to receive(:WfmSetLanguage)
allow(Yast::Language).to receive(:WfmSetGivenLanguage)
end

context "language needed to be switched to English" do
before do
allow(Yast::Language).to receive(:SwitchToEnglishIfNeeded).and_return(true)
end

it "switch language to english" do
expect(Yast::Language).to receive(:SwitchToEnglishIfNeeded).and_return(true)

subject.public_send(method)
end
end

context "language does not need to be switched to English" do
it "sets console font according to language" do
expect(Yast::Console).to receive(:SelectFont).with("cs_CZ")

subject.public_send(method)
end

it "sets WFM language according to selected language" do
expect(Yast::Language).to receive(:WfmSetLanguage)

subject.public_send(method)
end

context "selected langauge is nn_NO" do
before do
allow(Yast::Language).to receive(:language).and_return("nn_NO")
end

it "it sets WFM language to nb_NO instead" do
expect(Yast::Language).to receive(:WfmSetGivenLanguage).with("nb_NO")

subject.public_send(method)
end
end
end
end

it "enlists all available languages" do
expect(widget.items).to eq(LANGUAGES)
end

describe "#handle" do
let(:value) { "en_UK" }

before do
allow(Yast::Language).to receive(:language).and_return(default_language)
allow(widget).to receive(:value).and_return(value)
allow(Yast::Language).to receive(:Set)
allow(Yast::Language).to receive(:languages=)
allow(Yast::Timezone).to receive(:ResetZonemap)
end

context "when language remains unchanged" do
let(:value) { default_language }
it "returns nil" do
expect(widget.handle).to eq(nil)
end
end

context "when language has been changed" do
it "sets the new language" do
expect(Yast::Language).to receive(:Set).with(value)
expect(Yast::Language).to receive(:languages=).with(value)
widget.handle
end

it "resets the timezones map" do
expect(Yast::Timezone).to receive(:ResetZonemap)
widget.handle
end
end

context "when emit_event is set to true and Yast::Mode is not config" do
subject(:widget) { described_class.new(emit_event: true) }

include_examples "switch language", :handle

it "returns :redraw" do
expect(subject.handle).to eq :redraw
end
end
end

describe "#store" do
it "calls #handle method" do
expect(widget).to receive(:handle)
widget.store
end

context "when emit_event is set to false and Yast::Mode is not config" do
subject(:widget) { described_class.new(emit_event: false) }

include_examples "switch language", :store
end

end
end
7 changes: 7 additions & 0 deletions package/yast2-country.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Thu Sep 7 11:25:21 UTC 2017 - jreidinger@suse.com

- backport language selection widget to allow language selection
in CaaSP2 (FATE#323837)
- 3.2.12

-------------------------------------------------------------------
Thu Mar 23 18:45:42 UTC 2017 - igonzalezsosa@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-country.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-country
Version: 3.2.11
Version: 3.2.12
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -14,7 +14,7 @@
end

# for correct coverage report we need to load all ruby files
Dir["#{root_location}/*/src/{module,include,lib}/**/*.rb"].each { |f| require_relative f }
SimpleCov.track_files("#{root_location}/**/src/**/*.rb")

# use coveralls for on-line code coverage reporting at Travis CI
if ENV["TRAVIS"]
Expand Down

0 comments on commit ae6df11

Please sign in to comment.