Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output files with all-lowercase locale in filename #83

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace deprecated pkg/errors with errors package
Albert221 committed Dec 8, 2023
commit 02e4929873c7fdd08fd6357b3a3e3c5a31aaca90
3 changes: 1 addition & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import (

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

@@ -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{
11 changes: 5 additions & 6 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"
@@ -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"
)

@@ -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)
}
}

@@ -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))
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()

5 changes: 3 additions & 2 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"
)
@@ -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
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(
14 changes: 6 additions & 8 deletions convert/arb2poe/arb_parser.go
Original file line number Diff line number Diff line change
@@ -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
}

@@ -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
}

@@ -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
}

@@ -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 {
@@ -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]()
9 changes: 5 additions & 4 deletions convert/arb2poe/converter.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -30,7 +31,7 @@ var NoTermsError = 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
@@ -39,7 +40,7 @@ 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)
@@ -51,7 +52,7 @@ func (c *Converter) Convert(output io.Writer) (lang flutter.Locale, err error) {

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
9 changes: 5 additions & 4 deletions convert/poe2arb/converter.go
Original file line number Diff line number Diff line change
@@ -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"
)

@@ -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]()
@@ -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
}
@@ -100,7 +101,7 @@ func (c *Converter) Convert(output io.Writer) error {
encoder.SetIndent("", " ") // 4 spaces

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

func errorsToError(errs []error) error {
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"
)

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

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

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

@@ -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)
}
}

@@ -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)
@@ -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
7 changes: 3 additions & 4 deletions flutter/locale.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package flutter

import (
"fmt"
"slices"
"strings"

"github.com/pkg/errors"
)

type Locale struct {
@@ -48,7 +47,7 @@ func ParseLocale(locale string) (Locale, error) {
}, nil
case 3:
if !slices.Contains(knownScripts, strings.ToLower(parts[1])) {
return Locale{}, errors.Errorf("invalid script: %s", parts[1])
return Locale{}, fmt.Errorf(`invalid script "%s"`, parts[1])
}

return Locale{
@@ -57,7 +56,7 @@ func ParseLocale(locale string) (Locale, error) {
Country: strings.ToUpper(parts[2]),
}, nil
default:
return Locale{}, errors.Errorf("invalid locale: %s", locale)
return Locale{}, fmt.Errorf(`invalid locale "%s"`, locale)
}
}

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ go 1.21
require (
github.com/TwiN/go-color v1.4.0
github.com/caarlos0/env/v6 v6.10.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.1
github.com/wk8/go-ordered-map/v2 v2.1.1
gopkg.in/yaml.v3 v3.0.1
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -24,8 +24,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
20 changes: 9 additions & 11 deletions poeditor/client.go
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@ import (
"net/http"
"net/url"
"strings"

"github.com/pkg/errors"
)

const apiURL = "https://api.poeditor.com/v2"
@@ -50,19 +48,19 @@ func (c *Client) request(path string, params map[string]string, respBody interfa

req, err := http.NewRequest("POST", url, body)
if err != nil {
return errors.Wrap(err, "creating HTTP request")
return fmt.Errorf("creating HTTP request: %w", err)
}

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := c.client.Do(req)
if err != nil {
return errors.Wrap(err, "making HTTP request")
return fmt.Errorf("making HTTP request: %w", err)
}

err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return errors.Wrap(err, "decoding response")
return fmt.Errorf("decoding response: %w", err)
}

return nil
@@ -137,36 +135,36 @@ func (c *Client) Upload(projectID, languageCode string, file io.Reader) error {

fileWriter, err := w.CreateFormFile("file", "file.json")
if err != nil {
return errors.Wrap(err, "creating form field")
return fmt.Errorf("creating form field: %w", err)
}
if _, err = io.Copy(fileWriter, file); err != nil {
return errors.Wrap(err, "copying file to form field")
return fmt.Errorf("copying file to form field: %w", err)
}

w.WriteField("language", languageCode)
w.WriteField("overwrite", "0")

err = w.Close()
if err != nil {
return errors.Wrap(err, "closing multipart writer")
return fmt.Errorf("closing multipart writer: %w", err)
}

req, err := http.NewRequest("POST", url, &b)
if err != nil {
return errors.Wrap(err, "creating HTTP request")
return fmt.Errorf("creating HTTP request: %w", err)
}

req.Header.Set("Content-Type", w.FormDataContentType())

resp, err := c.client.Do(req)
if err != nil {
return errors.Wrap(err, "making HTTP request")
return fmt.Errorf("making HTTP request: %w", err)
}

var respBody baseResponse
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
return errors.Wrap(err, "decoding response")
return fmt.Errorf("decoding response: %w", err)
}

if err := handleRequestErr(err, respBody); err != nil {