-
Notifications
You must be signed in to change notification settings - Fork 15
/
Language.rb
1511 lines (1302 loc) · 47.4 KB
/
Language.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# ------------------------------------------------------------------------------
# Copyright (c) 2012 Novell, Inc. 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.
#
# 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.
# ------------------------------------------------------------------------------
# File: modules/Language.ycp
# Module: Language
# Summary: This module does all language related stuff:
# Authors: Klaus Kaempf <kkaempf@suse.de>
# Thomas Roelz <tom@suse.de>
# Maintainer: Jiri Suchomel <jsuchome@suse.cz>
#
# $Id$
require "yast"
require "yast2/execute"
require "shellwords"
module Yast
class LanguageClass < Module
DEFAULT_FALLBACK_LANGUAGE = "en_US".freeze
include Yast::Logger
require "y2country/language_dbus"
def main
Yast.import "Pkg"
Yast.import "UI"
textdomain "country"
Yast.import "AsciiFile"
Yast.import "Directory"
Yast.import "Encoding"
Yast.import "FileUtils"
Yast.import "InstExtensionImage"
Yast.import "Linuxrc"
Yast.import "Misc"
Yast.import "Mode"
Yast.import "PackageCallbacks"
Yast.import "PackageSystem"
Yast.import "Popup"
Yast.import "ProductFeatures"
Yast.import "Report"
Yast.import "SlideShow"
Yast.import "Stage"
# directory where all the language definitions are stored
# it's a constant, but depends on a dynamic content (Directory)
@languages_directory = "#{Directory.datadir}/languages"
# currently selected language
@language = DEFAULT_FALLBACK_LANGUAGE
# original language
@language_on_entry = DEFAULT_FALLBACK_LANGUAGE
# language preselected in /etc/install.inf
@preselected = DEFAULT_FALLBACK_LANGUAGE
# user readable description of language
@name = "English (US)"
@linuxrc_language_set = false
# Default language to be restored with MakeProposal.
@default_language = DEFAULT_FALLBACK_LANGUAGE
# Default settings for INSTALLED_LANGUAGES in /etc/sysconfig/language
@languages = ""
# Original value of INSTALLED_LANGUAGES
@languages_on_entry = ""
# Use utf8 in locale
@use_utf8 = true
# ncurses mode
@text_mode = nil
@ExpertSettingsChanged = false
# Was the initial language selection skipped? (see bug 223258)
# (It can be, if the language was selected in linuxrc)
@selection_skipped = false
# level of translation completeness
@translation_status = {}
# map (locale: 1) of available locales
@locales = {}
# map with all languages (cached - needs to be reread for retranslation)
@languages_map = {}
# mapping of language to its default (proposed) time zone
@lang2timezone = {}
# mapping of language to its default (proposed) kbd layout
@lang2keyboard = {}
# setting read from localed
@localed_conf = {}
# languages that cannot be correctly shown in text mode
# if the system (Linuxrc) does not start with them from the beginning
@cjk_languages = [
"ja",
"ko",
"zh",
"hi",
"km",
"pa",
"bn",
"gu",
"mr",
"si",
"ta",
"vi"
]
# FATE #302955: Split translations out of installation system
# [ "en_US", "en_GB", "de", "cs" ]
@available_lang_filenames = nil
# list of items for secondary languages term
@secondary_items = []
@english_names = {}
@reset_recommended = true
# language is read-only
@readonly = nil
Language()
end
#remove the suffix, if there's any (en_US.UTF-8 -> en_US)
def RemoveSuffix(lang)
return lang[/[a-zA-Z_]+/]
end
# Check if the language is "CJK"
# (and thus could not be shown in text mode - see bug #102958)
def CJKLanguage(lang)
l = Builtins.substring(lang, 0, 2)
Builtins.contains(@cjk_languages, l)
end
# return the value of text_mode (true for ncurses)
def GetTextMode
if @text_mode == nil
display_info = UI.GetDisplayInfo
@text_mode = Ops.get_boolean(display_info, "TextMode", false)
end
@text_mode
end
# Read language DB: translatable strings will be translated to current language
def read_languages_map
log.info("Reading languages map; @language: #{@language}")
Builtins.foreach(
Convert.convert(
SCR.Read(path(".target.dir"), @languages_directory, []),
:from => "any",
:to => "list <string>"
)
) do |file|
next if !Builtins.regexpmatch(file, "language_.+\\.ycp$")
language_map = Convert.to_map(
Builtins.eval(
SCR.Read(path(".target.yast2"), Ops.add("languages/", file))
)
)
language_map = {} if language_map == nil
code = file
Builtins.foreach(
Convert.convert(
language_map,
:from => "map",
:to => "map <string, any>"
)
) do |key, val|
if Ops.is_list?(val)
Ops.set(@languages_map, key, Convert.to_list(val))
code = key
end
end
if !Builtins.haskey(@lang2timezone, code)
Ops.set(
@lang2timezone,
code,
Ops.get_string(language_map, "timezone", "US/Eastern")
)
end
if !Builtins.haskey(@lang2keyboard, code)
Ops.set(
@lang2keyboard,
code,
Ops.get_string(language_map, "keyboard", DEFAULT_FALLBACK_LANGUAGE)
)
end
end
@languages_map = {} if @languages_map == nil
nil
end
# Read only the map of one language
# @param language code
def ReadLanguageMap(lang)
ret = {}
file = Builtins.sformat("language_%1.ycp", lang)
if FileUtils.Exists(Ops.add(Ops.add(@languages_directory, "/"), file))
ret = Convert.to_map(
Builtins.eval(
SCR.Read(path(".target.yast2"), Ops.add("languages/", file))
)
)
ret = {} if ret == nil
end
deep_copy(ret)
end
# Return the whole map with language descriptions
# @param [Boolean] force force new loading of the map from the files (forces the change
# of translations to current language)
def GetLanguagesMap(force)
read_languages_map if Builtins.size(@languages_map) == 0 || force
deep_copy(@languages_map)
end
# Return English translation of given language (Fate 301789)
def EnglishName(code, backup)
if Ops.get_string(@english_names, code, "") == ""
if @language == DEFAULT_FALLBACK_LANGUAGE
Ops.set(@english_names, code, backup)
else
Builtins.y2warning("nothing in english_names...")
end
end
Ops.get_string(@english_names, code, backup)
end
# Fill the map with English names of languages
def FillEnglishNames
old_lang = WFM.GetLanguage()
if @use_utf8
WFM.SetLanguage(DEFAULT_FALLBACK_LANGUAGE, "UTF-8")
else
WFM.SetLanguage(DEFAULT_FALLBACK_LANGUAGE)
end
Builtins.foreach(GetLanguagesMap(true)) do |code, info|
Ops.set(@english_names, code, Ops.get_string(info, 4, ""))
end
if @use_utf8
WFM.SetLanguage(old_lang, "UTF-8")
else
WFM.SetLanguage(old_lang)
end
nil
end
# return the content of lang2timezone map
# (mapping of languages to their default (proposed) time zones)
def GetLang2TimezoneMap(force)
read_languages_map if Builtins.size(@languages_map) == 0 && force
deep_copy(@lang2timezone)
end
# return the content of lang2keyboard map
# (mapping of languages to their default (proposed) keyboard layouts)
def GetLang2KeyboardMap(force)
read_languages_map if Builtins.size(@languages_map) == 0 && force
deep_copy(@lang2keyboard)
end
# return the map of all supported countries and language codes
def GetLocales
if @locales == nil || @locales == {}
out = SCR.Execute(path(".target.bash_output"), "/usr/bin/locale -a")
Builtins.foreach(
Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n")
) do |l|
pos = Builtins.findfirstof(l, ".@")
if pos != nil && Ops.greater_or_equal(pos, 0)
l = Builtins.substring(l, 0, pos)
end
Ops.set(@locales, l, 1) if l != ""
end
end
deep_copy(@locales)
end
# For given language, return the file name of language extension (image)
# to be downloaded to the inst-sys
# (FATE #302955: Split translations out of installation system)
def GetLanguageExtensionFilename(language)
if @available_lang_filenames == nil
lang_numbers = {}
Builtins.foreach(GetLanguagesMap(false)) do |code, data|
short = main_language(code)
if Ops.get(lang_numbers, short, 0) == 0
Ops.set(lang_numbers, short, 1)
else
Ops.set(
lang_numbers,
short,
Ops.add(Ops.get(lang_numbers, short, 0), 1)
)
end
end
@available_lang_filenames = Builtins.maplist(GetLanguagesMap(false)) do |code, data|
short = main_language(code)
if Ops.greater_than(Ops.get(lang_numbers, short, 0), 1)
next code
else
next short
end
end
end
check_for_languages = [language]
# 'en_US' ? add also 'en'
check_for_languages << main_language(language) if language.include?("_")
# Default fallback
filename = "yast2-trans-en_US.rpm"
Builtins.foreach(check_for_languages) do |one_language|
if Builtins.contains(@available_lang_filenames, one_language)
filename = Builtins.sformat("yast2-trans-%1.rpm", one_language)
raise Break
end
end
Builtins.y2milestone("Using %1 for %2", filename, language)
filename
end
# Downloads inst-sys extension for a given language
# including giving a UI feedback to the user
#
# @param [String] language, e.g. 'de_DE'
def integrate_inst_sys_extension(language)
log.info "integrating translation extension..."
# busy message
Popup.ShowFeedback(
"",
_("Downloading installation system language extension...")
)
InstExtensionImage.DownloadAndIntegrateExtension(
GetLanguageExtensionFilename(language)
)
Popup.ClearFeedback
log.info "integrating translation extension... done"
end
# Returns whether the given language string is supported by this library.
#
# @param [String] language
# @see @languages_directory
def valid_language?(language)
GetLanguagesMap(false).key?(language)
end
# Checks whether given language is supported by the installer
# and changes it to the default language en_US if it isn't.
#
# @param language [String] reference to the new language
# @param error_report [Boolean] showing an error popup
# @return [String] new (corrected) language
def correct_language(language, error_report: true)
# No correction needed, this is already a correct language definition
return language if valid_language?(language)
# TRANSLATORS: Error message. Strings marked %{...} will be replaced
# with variable content - do not translate them, please.
Report.Error(
_("Language '%{language}' was not found within the list of supported languages\n" +
"available at %{directory}.\n\nFallback language %{fallback} will be used."
) % {
:language => language,
:directory => @languages_directory,
:fallback => DEFAULT_FALLBACK_LANGUAGE
}
) if error_report
return DEFAULT_FALLBACK_LANGUAGE
end
# Changes the install.inf in inst-sys according to newly selected language
#
# FIXME: code just moved, refactoring needed
def adapt_install_inf
yinf = {}
yinf_ref = arg_ref(yinf)
AsciiFile.SetDelimiter(yinf_ref, " ")
yinf = yinf_ref.value
yinf_ref = arg_ref(yinf)
AsciiFile.ReadFile(yinf_ref, "/etc/yast.inf")
yinf = yinf_ref.value
lines = AsciiFile.FindLineField(yinf, 0, "Language:")
if Ops.greater_than(Builtins.size(lines), 0)
yinf_ref = arg_ref(yinf)
AsciiFile.ChangeLineField(
yinf_ref,
Ops.get_integer(lines, 0, -1),
1,
@language
)
yinf = yinf_ref.value
else
yinf_ref = arg_ref(yinf)
AsciiFile.AppendLine(yinf_ref, ["Language:", @language])
yinf = yinf_ref.value
end
yinf_ref = arg_ref(yinf)
AsciiFile.RewriteFile(yinf_ref, "/etc/yast.inf")
yinf = yinf_ref.value
end
# Set module to selected language.
#
# @param [String] lang language string ISO code of language
def Set(lang)
lang = deep_copy(lang)
if @language != lang
log.info("Language changed from #{@language} to #{lang}")
lang = correct_language(lang)
@language = lang
if Stage.initial && !Mode.test && !Mode.live_installation
integrate_inst_sys_extension(lang)
end
GetLocales() if Builtins.size(@locales) == 0
language_def = GetLanguagesMap(false).fetch(lang, [])
# In config mode, use language name translated into the current language
# othewrwise use the language name translated into that selected language
# because the whole UI will get translated later too
@name = (Mode.config ? language_def[4] : language_def[0]) || lang
Encoding.SetEncLang(@language)
else
log.info("Language unchanged: #{@language}")
end
if Stage.initial && !Mode.test
adapt_install_inf
# update "name" for proposal when it cannot be shown correctly
if GetTextMode() && CJKLanguage(lang) && !CJKLanguage(@preselected)
@name = GetLanguagesMap(false).fetch(lang, [])[1] || lang
end
end
nil
end
# Set the language that was read from sysconfig,
# read only one needed language file
def QuickSet(lang)
Builtins.y2milestone(
"original language: %1; setting to lang:%2",
@language,
lang
)
if @language != lang
lang_map = ReadLanguageMap(lang)
@name = Ops.get_string(lang_map, [lang, 0], lang)
@language = lang
Encoding.SetEncLang(@language)
end
nil
end
def LinuxrcLangSet
@linuxrc_language_set
end
# generate the whole locale string for given language according to DB
# (e.g. de_DE -> de_DE.UTF-8)
def GetLocaleString(lang)
# if the suffix is already there, do nothing
return lang if lang.count(".@") > 0
read_languages_map if Builtins.size(@languages_map) == 0
language_info = Ops.get(@languages_map, lang, [])
if !Builtins.haskey(@languages_map, lang)
language_info = [lang, lang, ".UTF-8"]
end
# full language code
idx = @use_utf8 ? 2 : 3
val = lang + (language_info[idx] || "")
Builtins.y2milestone("locale %1", val)
val
end
# Store current language as default language.
def SetDefault
Builtins.y2milestone("Setting default language: %1", @language)
@default_language = @language
nil
end
# read the localed.conf language
def ReadLocaleConfLanguage
return nil if Mode.testsuite
@localed_conf = Y2Country.read_locale_conf
return nil if @localed_conf.nil?
local_lang = @localed_conf["LANG"]
local_lang = local_lang.sub(/[.@].*$/, "") if local_lang
log.info("language from locale.conf: %{local_lang}")
local_lang
end
# Read the rest of language values from sysconfig
def ReadSysconfigValues
@languages = Misc.SysconfigRead(
path(".sysconfig.language.INSTALLED_LANGUAGES"),
""
)
nil
end
# read UTF-8 status from localed.conf
def ReadUtf8Setting
if Mode.testsuite
@use_utf8 = true
return nil
end
# during live installation, we have local configuration
if !Stage.initial || Mode.live_installation
@localed_conf = Y2Country.read_locale_conf
return nil if @localed_conf.nil?
local_lang = @localed_conf["LANG"]
@use_utf8 = local_lang.include?(".UTF-8") unless (local_lang.nil? || local_lang.empty?)
else
@use_utf8 = true
end
log.info("Use UTF-8: #{@use_utf8}")
end
# Constructor
#
# Initializes module either from /etc/install.inf
# or from /etc/sysconfig/language
def Language
if Mode.config
# read the translated name: bug #180633
read_languages_map
@name = Ops.get_string(@languages_map, [@language, 4], @language)
return
end
if Stage.initial && !Mode.live_installation
lang = ProductFeatures.GetStringFeature("globals", "language")
Builtins.y2milestone("product LANGUAGE %1", lang)
@preselected = Linuxrc.InstallInf("Locale")
Builtins.y2milestone("install_inf Locale %1", @preselected)
if @preselected != nil && @preselected != ""
lang = @preselected
@linuxrc_language_set = true if lang != DEFAULT_FALLBACK_LANGUAGE
else
@preselected = DEFAULT_FALLBACK_LANGUAGE
end
lang ||= ""
Builtins.y2milestone("lang after checking /etc/install.inf: %1", lang)
if lang == ""
# As language has not been set we are trying to ask libzypp.
# But libzypp can also returns languages which we do not support
# (e.g. default "en"). So we are checking and changing it to default
# if needed (without showing an error (bnc#1009508))
lang = correct_language(Pkg.GetTextLocale, error_report: false)
Builtins.y2milestone("setting lang to default language: %1", lang)
end
# Ignore any previous settings and take language from control file.
l = ProductFeatures.GetStringFeature("globals", "language")
if l != nil && l != ""
lang = l
Builtins.y2milestone(
"setting lang to ProductFeatures::language: %1",
lang
)
end
FillEnglishNames()
Set(lang) # coming from /etc/install.inf
SetDefault() # also default
else
local_lang = ReadLocaleConfLanguage() || @language
QuickSet(local_lang)
SetDefault() # also default
if Mode.live_installation || Stage.firstboot
FillEnglishNames()
end
end
if Ops.greater_than(
SCR.Read(path(".target.size"), "/etc/sysconfig/language"),
0
)
ReadSysconfigValues()
ReadUtf8Setting()
end
Encoding.SetUtf8Lang(@use_utf8)
nil
end
# Store the inital values; in normal mode, read from system was done in constructor
# @param [Boolean] really: also read the values from the system
def Read(really)
if really
Set(ReadLocaleConfLanguage() || @language)
ReadSysconfigValues()
ReadUtf8Setting()
end
@language_on_entry = @language
@languages_on_entry = @languages
Builtins.y2milestone(
"language: %1, languages: %2",
@language_on_entry,
@languages_on_entry
)
@ExpertSettingsChanged = false
true
end
# was anything modified?
def Modified
@language != @language_on_entry || @ExpertSettingsChanged ||
Builtins.sort(Builtins.splitstring(@languages, ",")) !=
Builtins.sort(Builtins.splitstring(@languages_on_entry, ","))
end
# Does the modification of language(s) require installation of new packages?
# This test compares the list of original languages (primary+secondary) with
# the list after user's modifications
def PackagesModified
Builtins.sort(
Builtins.union(Builtins.splitstring(@languages, ","), [@language])
) !=
Builtins.sort(
Builtins.union(
Builtins.splitstring(@languages_on_entry, ","),
[@language_on_entry]
)
)
end
# Return the values for the various expert settings in a map
#
# @param -
#
# @return [Hash] with values filled in
#
def GetExpertValues
{ "use_utf8" => @use_utf8 }
end
# Set the values of the various expert setting
#
# @param [Hash] val map with new values of expert settings
#
# @return [void]
#
def SetExpertValues(val)
val = deep_copy(val)
if Builtins.haskey(val, "use_utf8")
@use_utf8 = Ops.get_boolean(val, "use_utf8", false)
Encoding.SetUtf8Lang(@use_utf8)
end
nil
end
# Set the given language in WFM and UI
#
# @param language (could be different from current in CJK case)
#
# @return -
def WfmSetGivenLanguage(lang)
return if Mode.config
encoding = @use_utf8 ? "UTF-8" : Encoding.console
log.info("Language changed from #{@language} to #{lang} encoding: #{encoding} use_utf8: #{@use_utf8}")
UI.SetLanguage(lang, encoding)
if @use_utf8
WFM.SetLanguage(lang, "UTF-8")
else
WFM.SetLanguage(lang)
end
# Force rebuilding the languages map to make sure the correct translations are used
read_languages_map
nil
end
# Set the current language in WFM and UI
#
# @param -
#
# @return -
def WfmSetLanguage
log.info("Setting the current language")
WfmSetGivenLanguage(@language)
nil
end
# Return proposal string.
#
# @return [Array<String>] user readable description.
# If force_reset is true reset the module to the language
# stored in default_language.
def MakeProposal(force_reset, language_changed)
Builtins.y2milestone("force_reset: %1", force_reset)
Builtins.y2milestone("language_changed: %1", language_changed)
if force_reset
Set(@default_language) # reset
end
ret = [
# summary label
Builtins.sformat(_("Primary Language: %1"), @name)
]
if Builtins.size(@languages_map) == 0 || language_changed
read_languages_map
end
# maybe additional languages were selected in package selector (bnc#393007)
langs = Builtins.splitstring(@languages, ",")
missing = []
Builtins.foreach(Pkg.GetAdditionalLocales) do |additional|
# add the language for both kind of values ("cs" vs. "pt_PT")
if !Builtins.contains(langs, additional)
additional = DEFAULT_FALLBACK_LANGUAGE if additional == "en"
additional = "pt_PT" if additional == "pt"
if Builtins.haskey(@languages_map, additional)
missing = Builtins.add(missing, additional)
next
end
if Builtins.contains(langs, additional) #en_US or pt_PT already installed
next
end
# now, let's hope there's only one full entry for the short one
# (e.g. cs_CZ for cs)
Builtins.foreach(@languages_map) do |k, dummy|
if Builtins.substring(k, 0, 2) == additional
missing = Builtins.add(missing, k)
raise Break
end
end
end
end
if Ops.greater_than(Builtins.size(missing), 0)
langs = Convert.convert(
Builtins.union(langs, missing),
:from => "list",
:to => "list <string>"
)
@languages = Builtins.mergestring(langs, ",")
end
# now, generate the summary strings
if @languages != "" && @languages != @language
langs = []
Builtins.foreach(Builtins.splitstring(@languages, ",")) do |lang|
if lang != @language
l = Ops.get_string(
@languages_map,
[lang, 4],
Ops.get_string(@languages_map, [lang, 0], "")
)
langs = Builtins.add(langs, l) if l != ""
end
end
if Ops.greater_than(Builtins.size(langs), 0)
# summary label
ret = Builtins.add(
ret,
Builtins.sformat(
_("Additional Languages: %1"),
Builtins.mergestring(langs, ", ")
)
)
end
end
deep_copy(ret)
end
# Return 'simple' proposal string.
# @return [String] preformated description.
def MakeSimpleProposal
Yast.import "HTML"
ret = [
# summary label
Builtins.sformat(_("Primary Language: %1"), @name)
]
if @languages != "" && @languages != @language
langs = []
Builtins.foreach(Builtins.splitstring(@languages, ",")) do |lang|
if lang != @language
l = Ops.get_string(
@languages_map,
[lang, 4],
Ops.get_string(@languages_map, [lang, 0], "")
)
langs = Builtins.add(langs, l) if l != ""
end
end
if Ops.greater_than(Builtins.size(langs), 0)
# summary label
ret = Builtins.add(
ret,
Builtins.sformat(_("Additional Languages: %1"), HTML.List(langs))
)
end
end
HTML.List(ret)
end
# return user readable description of language
def GetName
@name
end
# Return a map of ids and names to build up a selection list
# for the user. The key is used later in the Set function
# to select this language. The name is a translated string.
#
# @return [Hash] of $[ language : [ utf8-name, ascii-name] ...]
# for all known languages
# 'language' is the (2 or 5 char) ISO language code.
# 'utf8-name' is a user-readable (UTF-8 encoded !) string.
# 'ascii-name' is an english (ascii encoded !) string.
# @see #Set
def Selection
read_languages_map
Builtins.mapmap(@languages_map) do |code, data|
{
code => [
Ops.get_string(data, 0, ""),
Ops.get_string(data, 1, ""),
Ops.get_string(data, 4, Ops.get_string(data, 0, ""))
]
}
end
end
# Save state to target.
def Save
SCR.Write(path(".sysconfig.language.INSTALLED_LANGUAGES"), @languages)
SCR.Write(path(".sysconfig.language"), nil)
run_locale_command(locale)
nil
rescue Cheetah::ExecutionFailed => e
log.error "Language configuration not written: #{e.inspect}"
log.error "stderr: #{e.stderr}"
# TRANSLATORS: the "%s" is replaced by the executed command
Report.Error(_("Could not save the language setting, the command\n%s\nfailed.") % e.commands.first.join(' '))
nil
end
# unselect all selected packages (bnc#439373)
#
# this is a workaround for installing recommened packages for already
# installed packages - we cannot simply set the solver flag
# as the laguage packages are also recommended, this would cause that
# no language package would be installed
#
# do this just once at the beginning
def ResetRecommendedPackages
return if !@reset_recommended
Pkg.PkgSolve(true)
# package names only, without version
names_only = true
selected_packages = Pkg.GetPackages(:selected, names_only)
log.info("Unselecting already recommended packages: #{selected_packages.inspect}")
selected_packages.each { |p| Yast::Pkg.PkgNeutral(p) }
@reset_recommended = false
nil
end
# Initializes source and target,
# computes the packages necessary to install and uninstall,
# @return false if the solver failed (unresolved dependencies)
def PackagesInit(selected_languages)
selected_languages = deep_copy(selected_languages)
PackageSystem.EnsureSourceInit
PackageSystem.EnsureTargetInit
solver_flags_backup = Pkg.GetSolverFlags
# first, do not ignore recommended (= also language) packages
Pkg.SetSolverFlags({ "ignoreAlreadyRecommended" => false })
# ... but skip non-language recommended packages
ResetRecommendedPackages()
Pkg.SetAdditionalLocales(selected_languages)
# now, add only recommended language packages (other recommended are PkgNeutral-ized)
solved = Pkg.PkgSolve(true)
Pkg.SetSolverFlags(solver_flags_backup)
solved
end
# checks for disk space (#50745)
# @return false when there is not enough disk space for new packages
def EnoughSpace
ok = true
Builtins.foreach(Pkg.TargetGetDU) do |mountpoint, usage|
if Ops.greater_than(Ops.get(usage, 2, 0), Ops.get(usage, 0, 0))
ok = false
end
end
ok
end
# Install and uninstall packages selected by Pkg::SetAdditionalLocales
def PackagesCommit
check_source
if !Mode.commandline
# work-around for following in order not to depend on yast2-packager
# PackageSlideShow::InitPkgData (false);
# "value" : PackageSlideShow::total_size_to_install / 1024 , // kilobytes
total_sizes_per_cd_per_src = Pkg.PkgMediaSizes
total_size_to_install = 0
Builtins.foreach(Builtins.flatten(total_sizes_per_cd_per_src)) do |item|
if item != -1
total_size_to_install = Ops.add(total_size_to_install, item)
end
end
SlideShow.Setup(
[
{
"name" => "packages",
"description" => _("Installing Packages..."),
"value" => Ops.divide(total_size_to_install, 1024), # kilobytes