Example #1
0
func TestTables(t *testing.T) {
	if !*long {
		return
	}

	gen.Init()

	// Read the CLDR zip file.
	r := gen.OpenCLDRCoreZip()
	defer r.Close()

	d := &cldr.Decoder{}
	d.SetDirFilter("supplemental", "main")
	d.SetSectionFilter("numbers")
	data, err := d.DecodeZip(r)
	if err != nil {
		t.Fatalf("DecodeZip: %v", err)
	}

	dr, err := cldr.ParseDraft(*draft)
	if err != nil {
		t.Fatalf("filter: %v", err)
	}

	for _, lang := range data.Locales() {
		p := message.NewPrinter(language.MustParse(lang))

		ldml := data.RawLDML(lang)
		if ldml.Numbers == nil || ldml.Numbers.Currencies == nil {
			continue
		}
		for _, c := range ldml.Numbers.Currencies.Currency {
			syms := cldr.MakeSlice(&c.Symbol)
			syms.SelectDraft(dr)

			for _, sym := range c.Symbol {
				cur, err := ParseISO(c.Type)
				if err != nil {
					continue
				}
				formatter := Symbol
				switch sym.Alt {
				case "":
				case "narrow":
					formatter = NarrowSymbol
				default:
					continue
				}
				want := sym.Data()
				if got := p.Sprint(formatter(cur)); got != want {
					t.Errorf("%s:%sSymbol(%s) = %s; want %s", lang, strings.Title(sym.Alt), c.Type, got, want)
				}
			}
		}
	}
}
Example #2
0
func TestFormatting(t *testing.T) {
	testCases := []struct {
		tag    language.Tag
		value  interface{}
		format Formatter
		want   string
	}{
		0: {en, USD.Amount(0.1), nil, "USD 0.10"},
		1: {en, XPT.Amount(1.0), Symbol, "XPT 1.00"},

		2: {en, USD.Amount(2.0), ISO, "USD 2.00"},
		3: {und, USD.Amount(3.0), Symbol, "US$ 3.00"},
		4: {en, USD.Amount(4.0), Symbol, "$ 4.00"},

		5: {en, USD.Amount(5.20), NarrowSymbol, "$ 5.20"},
		6: {en, AUD.Amount(6.20), Symbol, "A$ 6.20"},

		7: {en_AU, AUD.Amount(7.20), Symbol, "$ 7.20"},
		8: {en_AU, USD.Amount(8.20), Symbol, "US$ 8.20"},

		9:  {en, 9.0, Symbol.Default(EUR), "€ 9.00"},
		10: {en, 10.123, Symbol.Default(KRW), "₩ 10"},
		11: {fr, 11.52, Symbol.Default(TWD), "TWD 11.52"},
		12: {en, 12.123, Symbol.Default(czk), "CZK 12.12"},
		13: {en, 13.123, Symbol.Default(czk).Kind(Cash), "CZK 13"},
		14: {en, 14.12345, ISO.Default(MustParseISO("CLF")), "CLF 14.1235"},
		15: {en, USD.Amount(15.00), ISO.Default(TWD), "USD 15.00"},
		16: {en, KRW.Amount(16.00), ISO.Kind(Cash), "KRW 16"},

		// TODO: support integers as well.

		17: {en, USD, nil, "USD"},
		18: {en, USD, ISO, "USD"},
		19: {en, USD, Symbol, "$"},
		20: {en_AU, USD, Symbol, "US$"},
		21: {en_AU, USD, NarrowSymbol, "$"},
	}
	for i, tc := range testCases {
		p := message.NewPrinter(tc.tag)
		v := tc.value
		if tc.format != nil {
			v = tc.format(v)
		}
		if got := p.Sprint(v); got != tc.want {
			t.Errorf("%d: got %q; want %q", i, got, tc.want)
		}
	}
}
Example #3
0
func TestTables(t *testing.T) {
	testtext.SkipIfNotLong(t)

	// Read the CLDR zip file.
	r := gen.OpenCLDRCoreZip()
	defer r.Close()

	d := &cldr.Decoder{}
	d.SetDirFilter("supplemental", "main")
	d.SetSectionFilter("numbers")
	data, err := d.DecodeZip(r)
	if err != nil {
		t.Fatalf("DecodeZip: %v", err)
	}

	dr, err := cldr.ParseDraft(*draft)
	if err != nil {
		t.Fatalf("filter: %v", err)
	}

	for _, lang := range data.Locales() {
		p := message.NewPrinter(language.MustParse(lang))

		ldml := data.RawLDML(lang)
		if ldml.Numbers == nil || ldml.Numbers.Currencies == nil {
			continue
		}
		for _, c := range ldml.Numbers.Currencies.Currency {
			syms := cldr.MakeSlice(&c.Symbol)
			syms.SelectDraft(dr)

			for _, sym := range c.Symbol {
				cur, err := ParseISO(c.Type)
				if err != nil {
					continue
				}
				formatter := Symbol
				switch sym.Alt {
				case "":
				case "narrow":
					formatter = NarrowSymbol
				default:
					continue
				}
				want := sym.Data()
				if got := p.Sprint(formatter(cur)); got != want {
					t.Errorf("%s:%sSymbol(%s) = %s; want %s", lang, strings.Title(sym.Alt), c.Type, got, want)
				}
			}
		}
	}

	for _, reg := range data.Supplemental().CurrencyData.Region {
		i := 0
		for ; regionData[i].Region().String() != reg.Iso3166; i++ {
		}
		it := Query(Historical, NonTender, Region(language.MustParseRegion(reg.Iso3166)))
		for _, cur := range reg.Currency {
			from, _ := time.Parse("2006-01-02", cur.From)
			to, _ := time.Parse("2006-01-02", cur.To)

			it.Next()
			for j, r := range []QueryIter{&iter{regionInfo: &regionData[i]}, it} {
				if got, _ := r.From(); from != got {
					t.Errorf("%d:%s:%s:from: got %v; want %v", j, reg.Iso3166, cur.Iso4217, got, from)
				}
				if got, _ := r.To(); to != got {
					t.Errorf("%d:%s:%s:to: got %v; want %v", j, reg.Iso3166, cur.Iso4217, got, to)
				}
			}
			i++
		}
	}
}
Example #4
0
// SetLocale 设置程序的本地化语言信息为 tag
func SetLocale(tag language.Tag) {
	localePrinter = message.NewPrinter(tag)
}
Example #5
0
// Copyright 2016 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package locale

import (
	"io"

	"golang.org/x/text/language"
	"golang.org/x/text/message"
)

// 保证有个初始化的值,部分包的测试功能依赖此变量
var localePrinter *message.Printer = message.NewPrinter(language.Chinese)

// 各个语种的语言对照表,通过相应文件的 init() 作初始化这样
// 在删除文件是,就自动删除相应的语言文件,不需要手修改代码。
var locales = map[string]map[string]string{}

// 各个语言需要翻译的所有字符串
const (
	SyntaxError  = "在[%v:%v]出现语法错误[%v]"     // app/errors.go:23
	OptionsError = "配置文件[%v]中配置项[%v]错误[%v]" // app/errors.go:27

	// 与 flag 包相关的处理
	FlagUsage = `%v 是一个 RESTful API 文档生成工具。

参数:
%v