Skip to content

Commit

Permalink
Merge pull request #83 from leancodepl/fix/use-lowercase-arbs
Browse files Browse the repository at this point in the history
Output files with all-lowercase locale in filename
  • Loading branch information
Albert221 committed Dec 8, 2023
2 parents 8c0c307 + 2f51a24 commit d9a76eb
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
working-directory: integration_test
run: |
for dir in l10n l10n_prefixed; do
for locale in en pl en_HK sr sr_Cyrl es es_419; do
for locale in en pl en_hk sr sr_cyrl es es_419; do
echo "=== lib/$dir/app_$locale.arb ==="
cat lib/$dir/app_$locale.arb
done
Expand Down
3 changes: 1 addition & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/leancodepl/poe2arb/convert/poe2arb"
"github.com/leancodepl/poe2arb/flutter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,7 +44,7 @@ func runConvertIo(cmd *cobra.Command, args []string) error {

flutterLocale, err := flutter.ParseLocale(lang)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to parse locale %s", lang))
return fmt.Errorf("failed to parse locale %s: %w", lang, err)
}

conv := poe2arb.NewConverter(os.Stdin, &poe2arb.ConverterOptions{
Expand Down
13 changes: 6 additions & 7 deletions cmd/poe.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -12,7 +13,6 @@ import (
"github.com/leancodepl/poe2arb/flutter"
"github.com/leancodepl/poe2arb/log"
"github.com/leancodepl/poe2arb/poeditor"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -81,15 +81,14 @@ func runPoe(cmd *cobra.Command, args []string) error {
for _, lang := range langs {
flutterLocale, err := flutter.ParseLocale(lang.Code)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("parsing %s language code", lang.Code))
return fmt.Errorf("parsing %s language code: %w", lang.Code, err)
}

template := options.TemplateLocale == flutterLocale

err = poeCmd.ExportLanguage(lang, flutterLocale, template)
if err != nil {
msg := fmt.Sprintf("exporting %s (%s) language", lang.Name, lang.Code)
return errors.Wrap(err, msg)
return fmt.Errorf("exporting %s (%s) language: %w", lang.Name, lang.Code, err)
}
}

Expand Down Expand Up @@ -234,14 +233,14 @@ func (c *poeCommand) ExportLanguage(lang poeditor.Language, flutterLocale flutte
resp, err := http.Get(url)
if err != nil {
logSub.Error("making HTTP request failed: " + err.Error())
return errors.Wrap(err, "making HTTP request for export")
return fmt.Errorf("making HTTP request for export: %w", err)
}

filePath := path.Join(c.options.OutputDir, fmt.Sprintf("%s%s.arb", c.options.ARBPrefix, flutterLocale))
filePath := path.Join(c.options.OutputDir, fmt.Sprintf("%s%s.arb", c.options.ARBPrefix, flutterLocale.StringFilename()))
file, err := os.Create(filePath)
if err != nil {
logSub.Error("creating file failed: " + err.Error())
return errors.Wrap(err, "creating ARB file")
return fmt.Errorf("creating ARB file: %w", err)
}
defer file.Close()

Expand Down
7 changes: 4 additions & 3 deletions cmd/poe_options_selector.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package cmd

import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/leancodepl/poe2arb/flutter"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -126,7 +127,7 @@ func (s *poeOptionsSelector) SelectARBPrefixAndTemplate() (prefix string, templa
templateLocaleString := strings.TrimSuffix(strings.TrimPrefix(s.l10n.TemplateArbFile, prefix), ".arb")
templateLocale, err = flutter.ParseLocale(templateLocaleString)
if err != nil {
return "", flutter.Locale{}, errors.Wrap(err, "could not parse template locale")
return "", flutter.Locale{}, fmt.Errorf("could not parse template locale: %w", err)
}

return prefix, templateLocale, nil
Expand All @@ -152,7 +153,7 @@ func prefixFromTemplateFileName(templateFile string) (string, error) {

return "", errors.New(
"invalid template-arb-file. Should be a filename with prefix ending " +
"with an underscore followed by a valid BCP-47 locale.",
"with an underscore followed by a valid BCP-47 locale",
)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runSeed(cmd *cobra.Command, args []string) error {
var b bytes.Buffer
flutterLocale, err := converter.Convert(&b)
if err != nil {
if errors.Is(err, arb2poe.NoTermsError) {
if errors.Is(err, arb2poe.ErrNoTerms) {
fileLog.Info("no terms to convert")
continue
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func getVcsInfo() (revision, time string, modified bool, err error) {
}

for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
} else if setting.Key == "vcs.time" {
case "vcs.time":
time = setting.Value
} else if setting.Key == "vcs.modified" {
case "vcs.modified":
modified = setting.Value == "true"
}
}
Expand Down
2 changes: 1 addition & 1 deletion convert/arb2poe/arb_message_to_poe_term.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package arb2poe

import (
"errors"
"regexp"
"strings"

"github.com/leancodepl/poe2arb/convert"
"github.com/pkg/errors"
)

func arbMessageToPOETerm(
Expand Down
14 changes: 6 additions & 8 deletions convert/arb2poe/arb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package arb2poe

import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"

"github.com/leancodepl/poe2arb/convert"
"github.com/leancodepl/poe2arb/flutter"
"github.com/pkg/errors"
orderedmap "github.com/wk8/go-ordered-map/v2"
)

func parseARB(r io.Reader) (locale flutter.Locale, messages []*convert.ARBMessage, err error) {
var arb map[string]any
err = json.NewDecoder(r).Decode(&arb)
if err != nil {
err = errors.Wrap(err, "failed to decode ARB")
err = fmt.Errorf("failed to decode ARB: %w", err)
return flutter.Locale{}, nil, err
}

Expand All @@ -28,7 +28,7 @@ func parseARB(r io.Reader) (locale flutter.Locale, messages []*convert.ARBMessag

locale, err = flutter.ParseLocale(lang)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("parsing locale %s", lang))
err = fmt.Errorf("parsing locale %s: %w", lang, err)
return flutter.Locale{}, nil, err
}

Expand All @@ -39,7 +39,7 @@ func parseARB(r io.Reader) (locale flutter.Locale, messages []*convert.ARBMessag

var translation string
if translation, ok = value.(string); !ok {
err = errors.Errorf("invalid translation value for %s", key)
err = fmt.Errorf("invalid translation value for %s", key)
return flutter.Locale{}, nil, err
}

Expand All @@ -51,8 +51,7 @@ func parseARB(r io.Reader) (locale flutter.Locale, messages []*convert.ARBMessag
if attrs, ok := arb["@"+key].(map[string]any); ok {
encoded, err := json.Marshal(attrs)
if err != nil {
return flutter.Locale{}, nil,
errors.Wrap(err, fmt.Sprintf("failed to encode attributes for %s", key))
return flutter.Locale{}, nil, fmt.Errorf("failed to encode attributes for %s: %w", key, err)
}

var attributes struct {
Expand All @@ -63,8 +62,7 @@ func parseARB(r io.Reader) (locale flutter.Locale, messages []*convert.ARBMessag
}
err = json.Unmarshal(encoded, &attributes)
if err != nil {
return flutter.Locale{}, nil,
errors.Wrap(err, fmt.Sprintf("failed to decode attributes for %s", key))
return flutter.Locale{}, nil, fmt.Errorf("failed to decode attributes for %s: %w", key, err)
}

attrsOm := orderedmap.New[string, *convert.ARBPlaceholder]()
Expand Down
13 changes: 7 additions & 6 deletions convert/arb2poe/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package arb2poe

import (
"encoding/json"
"errors"
"fmt"
"io"

"github.com/leancodepl/poe2arb/convert"
"github.com/leancodepl/poe2arb/flutter"
"github.com/pkg/errors"
)

type Converter struct {
Expand All @@ -25,12 +26,12 @@ func NewConverter(input io.Reader, templateLocale flutter.Locale, termPrefix str
}
}

var NoTermsError = errors.New("no terms to convert")
var ErrNoTerms = errors.New("no terms to convert")

func (c *Converter) Convert(output io.Writer) (lang flutter.Locale, err error) {
lang, messages, err := parseARB(c.input)
if err != nil {
return flutter.Locale{}, errors.Wrap(err, "failed to parse ARB")
return flutter.Locale{}, fmt.Errorf("failed to parse ARB: %w", err)
}

template := c.templateLocale == lang
Expand All @@ -39,19 +40,19 @@ func (c *Converter) Convert(output io.Writer) (lang flutter.Locale, err error) {
for _, message := range messages {
poeTerm, err := arbMessageToPOETerm(message, !template, c.termPrefix)
if err != nil {
return flutter.Locale{}, errors.Wrapf(err, "decoding term %q failed", message.Name)
return flutter.Locale{}, fmt.Errorf("decoding term %q failed: %w", message.Name, err)
}

poeTerms = append(poeTerms, poeTerm)
}

if len(poeTerms) == 0 {
return flutter.Locale{}, NoTermsError
return flutter.Locale{}, ErrNoTerms
}

err = json.NewEncoder(output).Encode(poeTerms)
if err != nil {
return flutter.Locale{}, errors.Wrap(err, "failed to encode POEditor JSON")
return flutter.Locale{}, fmt.Errorf("failed to encode POEditor JSON: %w", err)
}

return lang, nil
Expand Down
13 changes: 9 additions & 4 deletions convert/poe2arb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package poe2arb

import (
"encoding/json"
"errors"
"fmt"
"io"
"regexp"
"strings"

"github.com/leancodepl/poe2arb/convert"
"github.com/leancodepl/poe2arb/flutter"
"github.com/pkg/errors"
orderedmap "github.com/wk8/go-ordered-map/v2"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ func (c *Converter) Convert(output io.Writer) error {
var jsonContents []*convert.POETerm
err := json.NewDecoder(c.input).Decode(&jsonContents)
if err != nil {
return errors.Wrap(err, "decoding json failed")
return fmt.Errorf("decoding json failed: %w", err)
}

arb := orderedmap.New[string, any]()
Expand All @@ -67,7 +68,7 @@ func (c *Converter) Convert(output io.Writer) error {

message, err := c.parseTerm(term)
if err != nil {
err = errors.Wrapf(err, `decoding term "%s" failed`, term.Term)
err = fmt.Errorf(`decoding term "%s" failed: %w`, term.Term, err)
errs = append(errs, err)
continue
}
Expand Down Expand Up @@ -100,7 +101,11 @@ func (c *Converter) Convert(output io.Writer) error {
encoder.SetIndent("", " ") // 4 spaces

err = encoder.Encode(arb)
return errors.Wrap(err, "encoding arb failed")
if err != nil {
return fmt.Errorf("encoding arb failed: %w", err)
} else {
return nil
}
}

func errorsToError(errs []error) error {
Expand Down
2 changes: 1 addition & 1 deletion convert/poe2arb/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package poe2arb

import (
"errors"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

Expand Down
9 changes: 5 additions & 4 deletions flutter/flutter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
package flutter

import (
"errors"
"fmt"
"os"
"path"
"path/filepath"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func NewFromDirectory(dir string) (*FlutterConfig, error) {
// l10n.yaml file found
err = yaml.NewDecoder(l10nFile).Decode(&l10n)
if err != nil {
return nil, errors.Wrap(err, "failure decoding l10n.yaml")
return nil, fmt.Errorf("failure decoding l10n.yaml: %w", err)
}
}

Expand All @@ -80,7 +81,7 @@ func walkUpForPubspec(dir string) (file *os.File, err error) {
}

if !errors.Is(err, os.ErrNotExist) {
return nil, errors.Wrap(err, "failure searching for pubspec.yaml")
return nil, fmt.Errorf("failure searching for pubspec.yaml: %w", err)
}

parent := filepath.Dir(dir)
Expand All @@ -98,7 +99,7 @@ func getL10nFile(pubspecDir string) (*os.File, error) {
file, err := os.Open(path.Join(pubspecDir, "l10n.yaml"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, errors.Wrap(err, "failure reading l10n.yaml")
return nil, fmt.Errorf("failure reading l10n.yaml: %w", err)
}

return nil, nil
Expand Down
Loading

0 comments on commit d9a76eb

Please sign in to comment.