-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from yast/language_selection_for_caasp
Language selection for caasp
- Loading branch information
Showing
9 changed files
with
286 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
language/src/lib/y2country/widgets/language_selection.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters