Skip to content

Commit

Permalink
move merge command
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder committed Feb 8, 2014
1 parent 1d237b3 commit 22d97cd
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 34 deletions.
13 changes: 6 additions & 7 deletions goi18n/goi18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"github.com/nicksnyder/go-i18n/i18n"
"os"
)

Expand Down Expand Up @@ -66,13 +65,13 @@ func main() {
format := flag.String("format", "json", "")
flag.Parse()

mc := &i18n.MergeCommand{
TranslationFiles: flag.Args(),
SourceLocaleID: *sourceLocale,
Outdir: *outdir,
Format: *format,
mc := &mergeCommand{
translationFiles: flag.Args(),
sourceLocaleID: *sourceLocale,
outdir: *outdir,
format: *format,
}
if err := mc.Execute(); err != nil {
if err := mc.execute(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
Expand Down
34 changes: 16 additions & 18 deletions i18n/merge.go → goi18n/merge.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i18n
package main

import (
"encoding/json"
Expand All @@ -13,38 +13,36 @@ import (
"sort"
)

// MergeCommand implements the merge functionality of the goi18n command.
type MergeCommand struct {
TranslationFiles []string
SourceLocaleID string
Outdir string
Format string
type mergeCommand struct {
translationFiles []string
sourceLocaleID string
outdir string
format string
}

// Execute executes the merge command.
func (mc *MergeCommand) Execute() error {
if len(mc.TranslationFiles) < 1 {
func (mc *mergeCommand) execute() error {
if len(mc.translationFiles) < 1 {
return fmt.Errorf("need at least one translation file to parse")
}

if _, err := locale.New(mc.SourceLocaleID); err != nil {
return fmt.Errorf("invalid source locale %s: %s", mc.SourceLocaleID, err)
if _, err := locale.New(mc.sourceLocaleID); err != nil {
return fmt.Errorf("invalid source locale %s: %s", mc.sourceLocaleID, err)
}

marshal, err := newMarshalFunc(mc.Format)
marshal, err := newMarshalFunc(mc.format)
if err != nil {
return err
}

bundle := bundle.New()
for _, tf := range mc.TranslationFiles {
for _, tf := range mc.translationFiles {
if err := bundle.LoadTranslationFile(tf); err != nil {
return fmt.Errorf("failed to load translation file %s because %s\n", tf, err)
}
}

translations := bundle.Translations()
sourceTranslations := translations[mc.SourceLocaleID]
sourceTranslations := translations[mc.sourceLocaleID]
for translationID, src := range sourceTranslations {
for _, localeTranslations := range translations {
if dst := localeTranslations[translationID]; dst == nil || reflect.TypeOf(src) != reflect.TypeOf(dst) {
Expand Down Expand Up @@ -77,13 +75,13 @@ func (mc *MergeCommand) Execute() error {

type marshalFunc func(interface{}) ([]byte, error)

func (mc *MergeCommand) writeFile(label string, translations []translation.Translation, localeID string, marshal marshalFunc) error {
func (mc *mergeCommand) writeFile(label string, translations []translation.Translation, localeID string, marshal marshalFunc) error {
sort.Sort(translation.SortableByID(translations))
buf, err := marshal(marshalInterface(translations))
if err != nil {
return fmt.Errorf("failed to marshal %s strings to %s because %s", localeID, mc.Format, err)
return fmt.Errorf("failed to marshal %s strings to %s because %s", localeID, mc.format, err)
}
filename := filepath.Join(mc.Outdir, fmt.Sprintf("%s.%s.%s", localeID, label, mc.Format))
filename := filepath.Join(mc.outdir, fmt.Sprintf("%s.%s.%s", localeID, label, mc.format))
if err := ioutil.WriteFile(filename, buf, 0666); err != nil {
return fmt.Errorf("failed to write %s because %s", filename, err)
}
Expand Down
14 changes: 7 additions & 7 deletions i18n/merge_test.go → goi18n/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i18n
package main

import (
"bytes"
Expand All @@ -17,13 +17,13 @@ func TestMergeExecute(t *testing.T) {
"testdata/input/ar-AR.two.json",
}

mc := &MergeCommand{
TranslationFiles: files,
SourceLocaleID: "en-US",
Outdir: "testdata/output",
Format: "json",
mc := &mergeCommand{
translationFiles: files,
sourceLocaleID: "en-US",
outdir: "testdata/output",
format: "json",
}
if err := mc.Execute(); err != nil {
if err := mc.execute(); err != nil {
t.Fatal(err)
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion i18n/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func Example() {
i18n.MustLoadTranslationFile("testdata/expected/en-US.all.json")
i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-US.all.json")

T, _ := i18n.Tfunc("en-US")

Expand Down
2 changes: 1 addition & 1 deletion i18n/exampletemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var tmpl = template.Must(template.New("").Funcs(funcMap).Parse(`
`))

func Example_template() {
i18n.MustLoadTranslationFile("testdata/expected/en-US.all.json")
i18n.MustLoadTranslationFile("../goi18n/testdata/expected/en-US.all.json")

T, _ := i18n.Tfunc("en-US")
tmpl.Funcs(map[string]interface{}{
Expand Down
30 changes: 30 additions & 0 deletions i18n/plural/plural_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package plural

import (
"testing"
)

func TestNewCategory(t *testing.T) {
tests := []struct {
src string
cat Category
err bool
}{
{"zero", Zero, false},
{"one", One, false},
{"two", Two, false},
{"few", Few, false},
{"many", Many, false},
{"other", Other, false},
{"asdf", Invalid, true},
}

for _, test := range tests {
cat, err := NewCategory(test.src)
wrongErr := (err != nil && !test.err) || (err == nil && test.err)
if cat != test.cat || wrongErr {
t.Errorf("New(%#v) returned %#v,%#v; expected %#v", test.src, cat, err, test.cat)
}
}

}

0 comments on commit 22d97cd

Please sign in to comment.