Skip to content

Commit

Permalink
Merge pull request #81 from leancodepl/bug/80-locale-dash-filename
Browse files Browse the repository at this point in the history
Fix dashes in ARBs filenames with locales having more than language
  • Loading branch information
Albert221 authored Dec 8, 2023
2 parents 6d618b0 + 7c0f254 commit 295638f
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 43 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ jobs:
- name: Print ARBs
working-directory: integration_test
run: |
echo "=== lib/l10n/app_en.arb ==="
cat lib/l10n/app_en.arb
echo "=== lib/l10n/app_pl.arb ==="
cat lib/l10n/app_pl.arb
echo "=== lib/l10n_prefixed/app_en.arb ==="
cat lib/l10n_prefixed/app_en.arb
echo "=== lib/l10n_prefixed/app_pl.arb ==="
cat lib/l10n_prefixed/app_pl.arb
for dir in l10n l10n_prefixed; 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
done
10 changes: 9 additions & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"fmt"
"os"

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

Expand Down Expand Up @@ -40,8 +43,13 @@ func runConvertIo(cmd *cobra.Command, args []string) error {
noTemplate, _ := cmd.Flags().GetBool(noTemplateFlag)
termPrefix, _ := cmd.Flags().GetString(termPrefixFlag)

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

conv := poe2arb.NewConverter(os.Stdin, &poe2arb.ConverterOptions{
Lang: lang,
Locale: flutterLocale,
Template: !noTemplate,
RequireResourceAttributes: true,
TermPrefix: termPrefix,
Expand Down
17 changes: 11 additions & 6 deletions cmd/poe.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,14 @@ func runPoe(cmd *cobra.Command, args []string) error {
}

for _, lang := range langs {
template := options.TemplateLocale == lang.Code
flutterLocale, err := flutter.ParseLocale(lang.Code)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("parsing %s language code", lang.Code))
}

template := options.TemplateLocale == flutterLocale

err := poeCmd.ExportLanguage(lang, template)
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)
Expand Down Expand Up @@ -178,7 +183,7 @@ func (c *poeCommand) GetExportLanguages() ([]poeditor.Language, error) {
var filteredLangs []poeditor.Language
for _, lang := range langs {
for _, overridenLang := range c.options.OverrideLangs {
if lang.Code == overridenLang {
if strings.EqualFold(lang.Code, overridenLang) {
filteredLangs = append(filteredLangs, lang)
break
}
Expand Down Expand Up @@ -219,7 +224,7 @@ func (c *poeCommand) EnsureOutputDirectory() error {
return nil
}

func (c *poeCommand) ExportLanguage(lang poeditor.Language, template bool) error {
func (c *poeCommand) ExportLanguage(lang poeditor.Language, flutterLocale flutter.Locale, template bool) error {
logSub := c.log.Info("fetching JSON export for %s (%s)", lang.Name, lang.Code).Sub()
url, err := c.client.GetExportURL(c.options.ProjectID, lang.Code)
if err != nil {
Expand All @@ -232,7 +237,7 @@ func (c *poeCommand) ExportLanguage(lang poeditor.Language, template bool) error
return errors.Wrap(err, "making HTTP request for export")
}

filePath := path.Join(c.options.OutputDir, fmt.Sprintf("%s%s.arb", c.options.ARBPrefix, lang.Code))
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())
Expand All @@ -243,7 +248,7 @@ func (c *poeCommand) ExportLanguage(lang poeditor.Language, template bool) error
convertLogSub := logSub.Info("converting JSON to ARB").Sub()

conv := poe2arb.NewConverter(resp.Body, &poe2arb.ConverterOptions{
Lang: lang.Code,
Locale: flutterLocale,
Template: template,
RequireResourceAttributes: c.options.RequireResourceAttributes,
TermPrefix: c.options.TermPrefix,
Expand Down
12 changes: 8 additions & 4 deletions cmd/poe_options_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type poeOptions struct {
TermPrefix string

ARBPrefix string
TemplateLocale string
TemplateLocale flutter.Locale
OutputDir string
OverrideLangs []string
RequireResourceAttributes bool
Expand Down Expand Up @@ -117,13 +117,17 @@ func (s *poeOptionsSelector) SelectTermPrefix() (string, error) {
}

// SelectARBPrefix returns ARB files prefix option from available sources.
func (s *poeOptionsSelector) SelectARBPrefixAndTemplate() (prefix, templateLocale string, err error) {
func (s *poeOptionsSelector) SelectARBPrefixAndTemplate() (prefix string, templateLocale flutter.Locale, err error) {
prefix, err = prefixFromTemplateFileName(s.l10n.TemplateArbFile)
if err != nil {
return "", "", err
return "", flutter.Locale{}, err
}

templateLocale = strings.TrimSuffix(strings.TrimPrefix(s.l10n.TemplateArbFile, prefix), ".arb")
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 prefix, templateLocale, nil
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func runSeed(cmd *cobra.Command, args []string) error {
converter := arb2poe.NewConverter(file, options.TemplateLocale, options.TermPrefix)

var b bytes.Buffer
lang, err := converter.Convert(&b)
flutterLocale, err := converter.Convert(&b)
if err != nil {
if errors.Is(err, arb2poe.NoTermsError) {
fileLog.Info("no terms to convert")
Expand All @@ -106,11 +106,12 @@ func runSeed(cmd *cobra.Command, args []string) error {
fileLog.Error("failed: " + err.Error())
return err
}
lang := flutterLocale.StringHyphen()

if len(options.OverrideLangs) > 0 {
langFound := false
for _, overridenLang := range options.OverrideLangs {
if lang == overridenLang {
if strings.EqualFold(lang, overridenLang) {
langFound = true
break
}
Expand All @@ -124,14 +125,14 @@ func runSeed(cmd *cobra.Command, args []string) error {

availableLangFound := false
for _, availableLang := range availableLangs {
if lang == availableLang.Code {
if strings.EqualFold(lang, availableLang.Code) {
availableLangFound = true
break
}
}

if !availableLangFound {
langLog := fileLog.Info("adding language %s to project", lang).Sub()
langLog := fileLog.Info("adding language %s to project", flutterLocale).Sub()

err = poeClient.AddLanguage(options.ProjectID, lang)
if err != nil {
Expand Down
21 changes: 15 additions & 6 deletions convert/arb2poe/arb_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import (
"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) (lang string, messages []*convert.ARBMessage, err error) {
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")
return "", nil, err
return flutter.Locale{}, nil, err
}

lang, ok := arb[convert.LocaleKey].(string)
if !ok {
err = errors.New("missing locale key")
return "", nil, err
return flutter.Locale{}, nil, err
}

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

for key, value := range arb {
Expand All @@ -33,7 +40,7 @@ func parseARB(r io.Reader) (lang string, messages []*convert.ARBMessage, err err
var translation string
if translation, ok = value.(string); !ok {
err = errors.Errorf("invalid translation value for %s", key)
return "", nil, err
return flutter.Locale{}, nil, err
}

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

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

attrsOm := orderedmap.New[string, *convert.ARBPlaceholder]()
Expand Down
3 changes: 2 additions & 1 deletion convert/arb2poe/arb_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"testing"

"github.com/leancodepl/poe2arb/flutter"
"github.com/stretchr/testify/assert"
)

Expand All @@ -13,7 +14,7 @@ func TestParseARB(t *testing.T) {

lang, messages, err := parseARB(file)

assert.Equal(t, "en", lang)
assert.Equal(t, flutter.Locale{Language: "en"}, lang)
assert.NotEmpty(t, messages)
assert.NoError(t, err)
}
15 changes: 8 additions & 7 deletions convert/arb2poe/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"io"

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

type Converter struct {
input io.Reader

templateLocale string
templateLocale flutter.Locale
termPrefix string
}

func NewConverter(input io.Reader, templateLocale, termPrefix string) *Converter {
func NewConverter(input io.Reader, templateLocale flutter.Locale, termPrefix string) *Converter {
return &Converter{
input: input,
templateLocale: templateLocale,
Expand All @@ -26,10 +27,10 @@ func NewConverter(input io.Reader, templateLocale, termPrefix string) *Converter

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

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

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

poeTerms = append(poeTerms, poeTerm)
}

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

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

return lang, nil
Expand Down
9 changes: 5 additions & 4 deletions convert/poe2arb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import (
"strings"

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

type Converter struct {
input io.Reader

lang string
locale flutter.Locale
template bool
requireResourceAttributes bool
termPrefix string
}

type ConverterOptions struct {
Lang string
Locale flutter.Locale
Template bool
RequireResourceAttributes bool
TermPrefix string
Expand All @@ -35,7 +36,7 @@ func NewConverter(
return &Converter{
input: input,

lang: options.Lang,
locale: options.Locale,
template: options.Template,
requireResourceAttributes: options.RequireResourceAttributes,
termPrefix: options.TermPrefix,
Expand All @@ -50,7 +51,7 @@ func (c *Converter) Convert(output io.Writer) error {
}

arb := orderedmap.New[string, any]()
arb.Set(convert.LocaleKey, c.lang)
arb.Set(convert.LocaleKey, c.locale.String())

prefixedRegexp := regexp.MustCompile("(?:([a-zA-Z]+):)?(.*)")
var errs []error
Expand Down
11 changes: 10 additions & 1 deletion convert/poe2arb/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/leancodepl/poe2arb/convert/poe2arb"
"github.com/leancodepl/poe2arb/flutter"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -84,6 +85,14 @@ func TestConverterConvert(t *testing.T) {
})
}

func flutterMustParseLocale(lang string) flutter.Locale {
locale, err := flutter.ParseLocale(lang)
if err != nil {
panic(err)
}
return locale
}

func convert(
t *testing.T,
input string,
Expand All @@ -93,7 +102,7 @@ func convert(
) (converted string, err error) {
reader := strings.NewReader(input)
conv := poe2arb.NewConverter(reader, &poe2arb.ConverterOptions{
Lang: "en",
Locale: flutterMustParseLocale("en"),
Template: template,
RequireResourceAttributes: requireResourceAttributes,
TermPrefix: termPrefix,
Expand Down
Loading

0 comments on commit 295638f

Please sign in to comment.