Browse Source

3 simple introductory demos with x/text.

Frédéric G. MARAND 1 năm trước cách đây
mục cha
commit
83a5924cc7

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 9 - 0
.idea/go__i18n_demo.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/go__i18n_demo.iml" filepath="$PROJECT_DIR$/.idea/go__i18n_demo.iml" />
+    </modules>
+  </component>
+</project>

+ 11 - 0
.idea/runConfigurations/xtext_01_basic.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="xtext/01 basic" type="GoApplicationRunConfiguration" factoryName="Go Application">
+    <module name="go__i18n_demo" />
+    <working_directory value="$PROJECT_DIR$/xtext" />
+    <kind value="FILE" />
+    <package value="xtext" />
+    <directory value="$PROJECT_DIR$" />
+    <filePath value="$PROJECT_DIR$/xtext/01basic/01basic.go" />
+    <method v="2" />
+  </configuration>
+</component>

+ 11 - 0
.idea/runConfigurations/xtext_02_plural.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="xtext/02 plural" type="GoApplicationRunConfiguration" factoryName="Go Application">
+    <module name="go__i18n_demo" />
+    <working_directory value="$PROJECT_DIR$/xtext" />
+    <kind value="FILE" />
+    <package value="xtext" />
+    <directory value="$PROJECT_DIR$" />
+    <filePath value="$PROJECT_DIR$/xtext/02plural/02plural.go" />
+    <method v="2" />
+  </configuration>
+</component>

+ 11 - 0
.idea/runConfigurations/xtext_03_interpolations.xml

@@ -0,0 +1,11 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="xtext/03 interpolations" type="GoApplicationRunConfiguration" factoryName="Go Application">
+    <module name="go__i18n_demo" />
+    <working_directory value="$PROJECT_DIR$/xtext" />
+    <kind value="FILE" />
+    <package value="xtext" />
+    <directory value="$PROJECT_DIR$" />
+    <filePath value="$PROJECT_DIR$/xtext/03interpolation/03interpolation.go" />
+    <method v="2" />
+  </configuration>
+</component>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 28 - 0
xtext/01basic/01basic.go

@@ -0,0 +1,28 @@
+package main
+
+import (
+	"log"
+
+	"golang.org/x/text/language"
+	"golang.org/x/text/message"
+)
+
+const (
+	Flowers = "There are %v flowers in our garden.\n"
+)
+
+func init() {
+	message.SetString(language.French, Flowers, "Il y a %d fleurs dans notre jardin.\n")
+}
+
+func main() {
+	pUK := message.NewPrinter(language.BritishEnglish)
+	pUK.Printf(Flowers, 1500)
+
+	frFR, err := language.Parse("fr_FR") // Also: ParseBase, ParseRegion, Composer
+	if err != nil {
+		log.Fatalf("parsing fr_FR: %v", err)
+	}
+	pFR := message.NewPrinter(frFR)
+	pFR.Printf(Flowers, 1500)
+}

+ 47 - 0
xtext/02plural/02plural.go

@@ -0,0 +1,47 @@
+package main
+
+import (
+	"fmt"
+	"log"
+
+	"golang.org/x/text/feature/plural"
+	"golang.org/x/text/language"
+	"golang.org/x/text/message"
+)
+
+const (
+	Problem = "You have %d problem.\n"
+)
+
+func init() {
+	message.Set(language.French, Problem,
+		plural.Selectf(1, "%d",
+			"=0", "Vous n'avez aucun problème\n",
+			"=1", "Vous avez un problème\n",
+			"other", "Vous avez %d problèmes\n",
+		),
+	)
+	message.Set(language.AmericanEnglish, Problem,
+		plural.Selectf(1, "%d",
+			"=0", "You do not have a problem\n",
+			"=1", "You have a problem\n",
+			"=2", "You have a couple of problems\n",
+			"other", "You have %d problems\n",
+		),
+	)
+}
+
+func main() {
+	french, err := language.Parse("fr_FR")
+	if err != nil {
+		log.Fatalf("parsing fr_FR= %v", err)
+	}
+	for _, lang := range []language.Tag{french, language.AmericanEnglish} {
+		fmt.Printf("Language: %v\n", lang)
+		p := message.NewPrinter(lang)
+		for i := 0; i < 4; i++ {
+			fmt.Print("\t")
+			p.Printf(Problem, i)
+		}
+	}
+}

+ 36 - 0
xtext/03interpolation/03interpolation.go

@@ -0,0 +1,36 @@
+package main
+
+import (
+	"fmt"
+
+	"golang.org/x/text/feature/plural"
+	"golang.org/x/text/language"
+	"golang.org/x/text/message"
+	"golang.org/x/text/message/catalog"
+)
+
+const (
+	Lateness = "You are %d minute(s) late\n"
+)
+
+func init() {
+	message.Set(language.French, Lateness,
+		catalog.Var("minutes", plural.Selectf(1, "%d",
+			"=1", "minute",
+			"other", "minutes",
+		)),
+		catalog.String("Vous êtes en retard de %[1]d ${minutes}\n"),
+	)
+}
+
+func main() {
+	for _, lang := range []language.Tag{language.French, language.AmericanEnglish} {
+		fmt.Printf("Language: %v\n", lang)
+		p := message.NewPrinter(lang)
+		for i := 1; i <= 2; i++ {
+			fmt.Print("\t")
+			p.Printf(Lateness, i)
+		}
+		fmt.Println()
+	}
+}

+ 5 - 0
xtext/go.mod

@@ -0,0 +1,5 @@
+module xtext
+
+go 1.20
+
+require golang.org/x/text v0.8.0

+ 2 - 0
xtext/go.sum

@@ -0,0 +1,2 @@
+golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
+golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=