Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple language selection widget
- Loading branch information
Showing
4 changed files
with
209 additions
and
7 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
113 changes: 113 additions & 0 deletions
113
src/lib/y2packager/widgets/simple_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,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 |
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 |
---|---|---|
@@ -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 |