Procházet zdrojové kódy

Better translate HELP entries and indexes

tags/v0.11.0-alpha
Daniel Oaks před 6 roky
rodič
revize
638b0c03fc
2 změnil soubory, kde provedl 81 přidání a 45 odebrání
  1. 76
    40
      irc/help.go
  2. 5
    5
      irc/server.go

+ 76
- 40
irc/help.go Zobrazit soubor

@@ -16,19 +16,20 @@ type HelpEntryType int
16 16
 
17 17
 const (
18 18
 	// CommandHelpEntry is a help entry explaining a client command.
19
-	CommandHelpEntry HelpEntryType = 0
19
+	CommandHelpEntry HelpEntryType = iota
20 20
 	// InformationHelpEntry is a help entry explaining general server info.
21
-	InformationHelpEntry HelpEntryType = 1
21
+	InformationHelpEntry
22 22
 	// ISupportHelpEntry is a help entry explaining a specific RPL_ISUPPORT token.
23
-	ISupportHelpEntry HelpEntryType = 2
23
+	ISupportHelpEntry
24 24
 )
25 25
 
26 26
 // HelpEntry represents an entry in the Help map.
27 27
 type HelpEntry struct {
28
-	oper      bool
29
-	text      string
30
-	helpType  HelpEntryType
31
-	duplicate bool
28
+	oper          bool
29
+	text          string
30
+	textGenerator func(*Client) string
31
+	helpType      HelpEntryType
32
+	duplicate     bool
32 33
 }
33 34
 
34 35
 // used for duplicates
@@ -508,8 +509,8 @@ Returns historical information on the last user with the given nickname.`,
508 509
 
509 510
 	// Informational
510 511
 	"modes": {
511
-		text:     cmodeHelpText + "\n\n" + umodeHelpText,
512
-		helpType: InformationHelpEntry,
512
+		textGenerator: modesTextGenerator,
513
+		helpType:      InformationHelpEntry,
513 514
 	},
514 515
 	"cmode": {
515 516
 		text:     cmodeHelpText,
@@ -564,26 +565,21 @@ Oragono supports the following channel membership prefixes:
564 565
 	},
565 566
 }
566 567
 
568
+// modesTextGenerator generates the text for the 'modes' help entry.
569
+// it exists only so we can translate this entry appropriately.
570
+func modesTextGenerator(client *Client) string {
571
+	return client.t(cmodeHelpText) + "\n\n" + client.t(umodeHelpText)
572
+}
573
+
567 574
 // HelpIndex contains the list of all help topics for regular users.
568
-var HelpIndex string
575
+var HelpIndex map[string]string
569 576
 
570 577
 // HelpIndexOpers contains the list of all help topics for opers.
571
-var HelpIndexOpers string
578
+var HelpIndexOpers map[string]string
572 579
 
573 580
 // GenerateHelpIndex is used to generate HelpIndex.
574
-func GenerateHelpIndex(forOpers bool) string {
575
-	newHelpIndex := `= Help Topics =
576
-
577
-Commands:
578
-%s
579
-
580
-RPL_ISUPPORT Tokens:
581
-%s
582
-
583
-Information:
584
-%s`
585
-
586
-	// generate them
581
+func GenerateHelpIndex(lm *LanguageManager, forOpers bool) map[string]string {
582
+	// generate the help entry lists
587 583
 	var commands, isupport, information []string
588 584
 
589 585
 	var line string
@@ -606,34 +602,58 @@ Information:
606 602
 		}
607 603
 	}
608 604
 
609
-	// sort the lines
605
+	// create the strings
610 606
 	sort.Strings(commands)
607
+	commandsString := strings.Join(commands, "\n")
611 608
 	sort.Strings(isupport)
609
+	isupportString := strings.Join(isupport, "\n")
612 610
 	sort.Strings(information)
611
+	informationString := strings.Join(information, "\n")
613 612
 
614 613
 	// sub them in
615
-	newHelpIndex = fmt.Sprintf(newHelpIndex, strings.Join(commands, "\n"), strings.Join(isupport, "\n"), strings.Join(information, "\n"))
614
+	defaultHelpIndex := `= Help Topics =
615
+
616
+Commands:
617
+%s
618
+
619
+RPL_ISUPPORT Tokens:
620
+%s
621
+
622
+Information:
623
+%s`
624
+
625
+	newHelpIndex := make(map[string]string)
626
+
627
+	newHelpIndex["en"] = fmt.Sprintf(defaultHelpIndex, commandsString, isupportString, informationString)
628
+
629
+	lm.RLock()
630
+	defer lm.RUnlock()
631
+
632
+	for langCode := range lm.Info {
633
+		translatedHelpIndex := lm.Translate([]string{langCode}, defaultHelpIndex)
634
+		if translatedHelpIndex != defaultHelpIndex {
635
+			newHelpIndex[langCode] = fmt.Sprintf(translatedHelpIndex, commandsString, isupportString, informationString)
636
+		}
637
+	}
616 638
 
617 639
 	return newHelpIndex
618 640
 }
619 641
 
620 642
 // GenerateHelpIndices generates our help indexes and confirms we have HELP entries for every command.
621
-func GenerateHelpIndices() error {
622
-	if HelpIndex != "" && HelpIndexOpers != "" {
623
-		return nil
624
-	}
625
-
643
+func GenerateHelpIndices(lm *LanguageManager) error {
626 644
 	// startup check that we have HELP entries for every command
627
-	for name := range Commands {
628
-		_, exists := Help[strings.ToLower(name)]
629
-		if !exists {
630
-			return fmt.Errorf("Help entry does not exist for command %s", name)
645
+	if len(HelpIndex) == 0 && len(HelpIndexOpers) == 0 {
646
+		for name := range Commands {
647
+			_, exists := Help[strings.ToLower(name)]
648
+			if !exists {
649
+				return fmt.Errorf("Help entry does not exist for command %s", name)
650
+			}
631 651
 		}
632 652
 	}
633 653
 
634 654
 	// generate help indexes
635
-	HelpIndex = GenerateHelpIndex(false)
636
-	HelpIndexOpers = GenerateHelpIndex(true)
655
+	HelpIndex = GenerateHelpIndex(lm, false)
656
+	HelpIndexOpers = GenerateHelpIndex(lm, true)
637 657
 	return nil
638 658
 }
639 659
 
@@ -656,6 +676,18 @@ func (client *Client) sendHelp(name string, text string) {
656 676
 	client.Send(nil, client.server.name, RPL_ENDOFHELP, args...)
657 677
 }
658 678
 
679
+// GetHelpIndex returns the help index for the given language.
680
+func GetHelpIndex(languages []string, helpIndex map[string]string) string {
681
+	for _, lang := range languages {
682
+		index, exists := helpIndex[lang]
683
+		if exists {
684
+			return index
685
+		}
686
+	}
687
+	// 'en' always exists
688
+	return helpIndex["en"]
689
+}
690
+
659 691
 // helpHandler returns the appropriate help for the given query.
660 692
 func helpHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
661 693
 	argument := strings.ToLower(strings.TrimSpace(strings.Join(msg.Params, " ")))
@@ -670,9 +702,9 @@ Get an explanation of <argument>, or "index" for a list of help topics.`))
670 702
 	// handle index
671 703
 	if argument == "index" {
672 704
 		if client.flags[Operator] {
673
-			client.sendHelp("HELP", HelpIndexOpers)
705
+			client.sendHelp("HELP", GetHelpIndex(client.languages, HelpIndexOpers))
674 706
 		} else {
675
-			client.sendHelp("HELP", HelpIndex)
707
+			client.sendHelp("HELP", GetHelpIndex(client.languages, HelpIndex))
676 708
 		}
677 709
 		return false
678 710
 	}
@@ -680,7 +712,11 @@ Get an explanation of <argument>, or "index" for a list of help topics.`))
680 712
 	helpHandler, exists := Help[argument]
681 713
 
682 714
 	if exists && (!helpHandler.oper || (helpHandler.oper && client.flags[Operator])) {
683
-		client.sendHelp(strings.ToUpper(argument), helpHandler.text)
715
+		if helpHandler.textGenerator != nil {
716
+			client.sendHelp(strings.ToUpper(argument), client.t(helpHandler.textGenerator(client)))
717
+		} else {
718
+			client.sendHelp(strings.ToUpper(argument), client.t(helpHandler.text))
719
+		}
684 720
 	} else {
685 721
 		args := msg.Params
686 722
 		args = append(args, client.t("Help not found"))

+ 5
- 5
irc/server.go Zobrazit soubor

@@ -135,11 +135,6 @@ type clientConn struct {
135 135
 
136 136
 // NewServer returns a new Oragono server.
137 137
 func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
138
-	// TODO move this to main?
139
-	if err := GenerateHelpIndices(); err != nil {
140
-		return nil, err
141
-	}
142
-
143 138
 	// initialize data structures
144 139
 	server := &Server{
145 140
 		accounts:            make(map[string]*ClientAccount),
@@ -161,6 +156,11 @@ func NewServer(config *Config, logger *logger.Manager) (*Server, error) {
161 156
 		return nil, err
162 157
 	}
163 158
 
159
+	// generate help info
160
+	if err := GenerateHelpIndices(server.languages); err != nil {
161
+		return nil, err
162
+	}
163
+
164 164
 	// Attempt to clean up when receiving these signals.
165 165
 	signal.Notify(server.signals, ServerExitSignals...)
166 166
 	signal.Notify(server.rehashSignal, syscall.SIGHUP)

Načítá se…
Zrušit
Uložit