Skip to content

Commit

Permalink
Merge ff85e2c into 6fa0d56
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Jun 14, 2019
2 parents 6fa0d56 + ff85e2c commit 46c8717
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 194 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# 2 space indentation
[*.rb]
indent_style = space
indent_size = 2
7 changes: 7 additions & 0 deletions package/yast2-packager.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Thu Jun 13 17:15:37 CEST 2019 - David Diaz <dgonzalez@suse.com>

- Allow to select the license language when running in textmode
(bsc#1135901)
- 4.1.45

-------------------------------------------------------------------
Thu May 30 12:59:43 UTC 2019 - Ladislav Slezák <lslezak@suse.cz>

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


Name: yast2-packager
Version: 4.1.44
Version: 4.1.45
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down Expand Up @@ -137,6 +137,7 @@ rake install DESTDIR="%{buildroot}"
%{yast_ybindir}/*
%{yast_yncludedir}/checkmedia/*
%{yast_yncludedir}/packager/*
%{yast_libdir}/language_tag.rb
%{yast_libdir}/packager/*
%{yast_libdir}/packager/cfa/*
%{yast_libdir}/y2packager/*
Expand Down
99 changes: 99 additions & 0 deletions src/lib/language_tag.rb
@@ -0,0 +1,99 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2019 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"

# {::Comparable} enforces a total ordering, contrary to its
# documentation, WTF.
module PartiallyComparable
def <(other)
cmp = self.<=>(other)
return nil if cmp.nil?
cmp < 0
end

def >(other)
cmp = self.<=>(other)
return nil if cmp.nil?
cmp > 0
end

def <=(other)
cmp = self.<=>(other)
return nil if cmp.nil?
cmp <= 0
end

def >=(other)
cmp = self.<=>(other)
return nil if cmp.nil?
cmp >= 0
end

def ==(other)
return true if equal?(other) # object identity
cmp = self.<=>(other)
return nil if cmp.nil?
cmp == 0
end
end

# Language tags like "cs" "cs_CZ" "cs_CZ.UTF-8".
#
# FIXME: improve the simplistic string comparisons
class LanguageTag
include Yast::Logger

# @param s [String]
def initialize(s)
@tag = s
end

def to_s
@tag
end

include PartiallyComparable

# Like with classes (where Special < General) "en_US" < "en"
# Mnemonics: number of speakers
def <=>(other)
return 0 if to_s == other.to_s
return -1 if to_s.start_with?(other.to_s)
return 1 if other.to_s.start_with?(to_s)
nil
end

# A more general tag: "en_US" -> "en" (-> nil)
# @return [LanguageTag,nil]
def generalize
self.class.new(@tag.split("_").first) if @tag.include? "_"
# else nil
# FIXME: or self, find out what makes more sense
end

# @return [String,nil]
def name(lang_map_cache: nil)
lang_map_cache ||= Yast::Language.GetLanguagesMap(false)
attrs = lang_map_cache[@tag]
if attrs.nil?
# we're en, find en_US
_tag, attrs = lang_map_cache.find { |k, _v| self > LanguageTag.new(k) }
end
if attrs.nil?
log.warn "Could not find name for language '#{@tag}'"
return nil
end

attrs[4]
end
end
44 changes: 17 additions & 27 deletions src/lib/y2packager/widgets/product_license_translations.rb
Expand Up @@ -12,6 +12,7 @@

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

Expand Down Expand Up @@ -71,7 +72,7 @@ def handle(event)
# @return [Y2Packager::Widgets::SimpleLanguageSelection]
def language_selection
@language_selection ||=
Y2Packager::Widgets::SimpleLanguageSelection.new(available_locales, content_language)
Y2Packager::Widgets::SimpleLanguageSelection.new(selectable_locales, content_language)
end

# Product selection widget
Expand All @@ -82,49 +83,38 @@ def product_license
Y2Packager::Widgets::ProductLicenseContent.new(product, content_language)
end

# Available license translations
# Selectable license translations
#
# When running on textmode, only the preselected/given language is considered.
# When running on textmode, the terminal is not able to display *some* languages
# see #default_language for further details.
#
# @return [Array<String>] Locale codes of the available translations
# @see #default_language
def available_locales
Yast::UI.TextMode ? [default_language] : product.license_locales
def selectable_locales
product.license_locales.find_all { |loc| displayable_language?(loc) }
end

# License translation language
#
# When running on textmode, it returns the preselected/default language.
# see #default_language for further details.
# If the wanted language is present among those displayable, use it,
# otherwise use the default
#
# @return [String] License content language
# @see #default_language
def content_language
Yast::UI.TextMode ? default_language : language
# this selects "en" if we want "en_US"
l = selectable_locales.find { |loc| LanguageTag.new(loc) >= language }
l || DEFAULT_FALLBACK_LANGUAGE
end

# @return [String] Fallback language
DEFAULT_FALLBACK_LANGUAGE = "en_US".freeze

# Default language
#
# For some languages (like Japanese, Chinese or Korean) YaST needs to use a fbiterm in order
# to display symbols correctly when running on textmode. However, if none of those languages
# is selected on boot, this special terminal won't be used.
#
# So during 1st stage and when running in textmode, it returns the preselected language (from
# install.inf).
#
# On an installed system, it prefers the given language. Finally, if the license translation
# is not available, the fallback language is returned.
# Whether a language is displayable
#
# @return [String] Language code
def default_language
candidate_lang = Yast::Stage.initial ? Yast::Language.preselected : language
translated = product.license_locales.any? { |l| candidate_lang.start_with?(l) }
return candidate_lang if translated
DEFAULT_FALLBACK_LANGUAGE
# @param lang [String] "cs" or "cs_CZ"
# @return [Boolean]
# @see Yast::Language.supported_language?
def displayable_language?(lang)
Yast::Language.supported_language?(lang)
end
end
end
Expand Down
30 changes: 10 additions & 20 deletions src/lib/y2packager/widgets/simple_language_selection.rb
Expand Up @@ -19,6 +19,7 @@

require "yast"
require "cwm/widget"
require "language_tag"

Yast.import "Language"

Expand Down Expand Up @@ -69,14 +70,11 @@ def opt
# 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

candidates = [
default,
LanguageTag.new(default).generalize.to_s
]
new_value = candidates.compact.find { |c| languages.include?(c) }
self.value = new_value || DEFAULT_LICENSE_LANG
end

Expand All @@ -92,19 +90,11 @@ def help
# @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]]
lmap = Yast::Language.GetLanguagesMap(false)
@items = languages.map do |lang|
[lang, LanguageTag.new(lang).name(lang_map_cache: lmap)]
end
@items.reject! { |_lang, name| name.nil? }
@items.uniq!
@items.sort_by!(&:last)
end
Expand Down

0 comments on commit 46c8717

Please sign in to comment.