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

Add fill-in option #58

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion cmd/translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ translations.swift:
cmd.Flags().StringVarP(&flags.Kind, "type", "t", "", "The type of output to generate, valid options are 'ios' or 'android' (Required)")
cmd.Flags().IntVarP(&flags.KeyIndex, "index", "k", 0, "The index of the key row, defaults to 0")
cmd.Flags().StringArrayVarP(&configurations, "configuration", "c", make([]string, 0), "A configuration string consisting of space separated row index and output path. Multiple configurations can be added, but one is required")
cmd.Flags().IntVarP(&flags.DefaultValueIndex, "main-index", "m", 0, "Required for ios. The index of the main/default language row, defaults to 0")
cmd.Flags().IntVarP(&flags.DefaultValueIndex, "main-index", "m", 0, "Required for ios or when using fill-in. The index of the main/default language row, defaults to 0")
cmd.Flags().StringVarP(&flags.Output, "output", "o", "", "Required for ios. A path for the generated output")
cmd.Flags().BoolVarP(&flags.FillIn, "fill-in", "l", false, "Fill in the value from the main/default language if a value is missing for the current language")
cmd.MarkFlagRequired("input")
cmd.MarkFlagRequired("kind")
cmd.MarkFlagRequired("configuration")
Expand Down
3 changes: 2 additions & 1 deletion docs/generated/lane_translations_generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ lane translations generate [flags]

```
-c, --configuration stringArray A configuration string consisting of space separated row index and output path. Multiple configurations can be added, but one is required
-l, --fill-in Fill in the value from the main/default language if a value is missing for the current language
-h, --help help for generate
-k, --index int The index of the key row, defaults to 0
-i, --input string Path to a CSV file containing a key row and a row for each language (Required)
-m, --main-index int Required for ios. The index of the main/default language row, defaults to 0
-m, --main-index int Required for ios or when using fill-in. The index of the main/default language row, defaults to 0
-o, --output string Required for ios. A path for the generated output
-t, --type string The type of output to generate, valid options are 'ios' or 'android' (Required)
```
Expand Down
1 change: 1 addition & 0 deletions internal/translations/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Flags struct {
KeyIndex int
DefaultValueIndex int
Output string
FillIn bool
}

func (f *Flags) validate() error {
Expand Down
53 changes: 53 additions & 0 deletions internal/translations/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,56 @@ func TestIos(t *testing.T) {
equalFiles(t, "testdata/ios-da.expected", "../../build/da.strings")
equalFiles(t, "testdata/ios-swift.expected", "../../build/Translations.swift")
}

func TestAndroidWithFillIn(t *testing.T) {
flags := Flags{
Input: "testdata/fill-in/input.csv",
Kind: "android",
KeyIndex: 1,
DefaultValueIndex: 2,
FillIn: true,
}
configurations := []string{"2 ../../build/en.xml", "3 ../../build/da.xml"}

err := Generate(context.Background(), &flags, configurations)

assert.Nil(t, err)
equalFiles(t, "testdata/fill-in/android-en.expected", "../../build/en.xml")
equalFiles(t, "testdata/fill-in/android-da.expected", "../../build/da.xml")
}

func TestJsonWithFillIn(t *testing.T) {
flags := Flags{
Input: "testdata/fill-in/input.csv",
Kind: "json",
KeyIndex: 1,
DefaultValueIndex: 2,
FillIn: true,
}
configurations := []string{"2 ../../build/en.json", "3 ../../build/da.json"}

err := Generate(context.Background(), &flags, configurations)

assert.Nil(t, err)
equalFiles(t, "testdata/fill-in/json-en.expected", "../../build/en.json")
equalFiles(t, "testdata/fill-in/json-da.expected", "../../build/da.json")
}

func TestIosWithFillIn(t *testing.T) {
flags := Flags{
Input: "testdata/fill-in/input.csv",
Kind: "ios",
KeyIndex: 1,
DefaultValueIndex: 2,
Output: "../../build/Translations.swift",
FillIn: true,
}
configurations := []string{"2 ../../build/en.strings", "3 ../../build/da.strings"}

err := Generate(context.Background(), &flags, configurations)

assert.Nil(t, err)
equalFiles(t, "testdata/fill-in/ios-en.expected", "../../build/en.strings")
equalFiles(t, "testdata/fill-in/ios-da.expected", "../../build/da.strings")
equalFiles(t, "testdata/fill-in/ios-swift.expected", "../../build/Translations.swift")
}
39 changes: 22 additions & 17 deletions internal/translations/language_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
)

type languageFile struct {
path string
keyIndex int
valueIndex int
path string
keyIndex int
valueIndex int
useFallback bool
fallbackIndex int
}

type languageFileWriter interface {
Expand All @@ -21,7 +23,7 @@ type languageFileWriter interface {
}

func (f *languageFile) write(writer languageFileWriter, translations *translationData) error {
translation := translations.translation(f.keyIndex, f.valueIndex)
translation := translations.translation(f.keyIndex, f.valueIndex, f.useFallback, f.fallbackIndex)

tempPath := f.path + ".tmp"
defer os.Remove(tempPath)
Expand Down Expand Up @@ -65,31 +67,23 @@ func newLanguageFiles(flags *Flags, configurations []string) ([]languageFileWrit
arguments[path] = index
}

once := true
list := make([]languageFileWriter, 0)
for path, index := range arguments {
var writer languageFileWriter

file := &languageFile{
path: path,
keyIndex: flags.KeyIndex,
valueIndex: index,
path: path,
keyIndex: flags.KeyIndex,
valueIndex: index,
useFallback: flags.FillIn,
fallbackIndex: flags.DefaultValueIndex,
}

switch flags.Kind {
case androidKind:
writer = &androidLanguageFile{file: file}
case iosKind:
writer = &iosLanguageFile{file: file}
if once {
once = false
supporter := &iosSupportLanguageFile{file: &languageFile{
path: flags.Output,
keyIndex: flags.KeyIndex,
valueIndex: flags.DefaultValueIndex,
}}
list = append(list, supporter)
}
case jsonKind:
writer = &jsonLanguageFile{file: file}
default:
Expand All @@ -99,5 +93,16 @@ func newLanguageFiles(flags *Flags, configurations []string) ([]languageFileWrit
list = append(list, writer)
}

switch flags.Kind {
case iosKind:
supporter := &iosSupportLanguageFile{file: &languageFile{
path: flags.Output,
keyIndex: flags.KeyIndex,
valueIndex: flags.DefaultValueIndex,
useFallback: false,
}}
list = append(list, supporter)
}

return list, nil
}
2 changes: 1 addition & 1 deletion internal/translations/language_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestLanguageFileWriteInputFile(t *testing.T) {
}

for _, x := range inputWriters {
tr := translations.translation(1, x.index)
tr := translations.translation(1, x.index, false, 0)
var b bytes.Buffer

err := x.writer.write(tr, &b)
Expand Down
4 changes: 4 additions & 0 deletions internal/translations/testdata/fill-in/android-da.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="something">Noget</string>
<string name="something_only_in_en">Something only in English</string>
</resources>
4 changes: 4 additions & 0 deletions internal/translations/testdata/fill-in/android-en.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="something">Something</string>
<string name="something_only_in_en">Something only in English</string>
</resources>
3 changes: 3 additions & 0 deletions internal/translations/testdata/fill-in/input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
KEY,EN,DA
SOMETHING,Something,Noget
SOMETHING_ONLY_IN_EN,"Something only in English",
2 changes: 2 additions & 0 deletions internal/translations/testdata/fill-in/ios-da.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"SOMETHING" = "Noget";
"SOMETHING_ONLY_IN_EN" = "Something only in English";
2 changes: 2 additions & 0 deletions internal/translations/testdata/fill-in/ios-en.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"SOMETHING" = "Something";
"SOMETHING_ONLY_IN_EN" = "Something only in English";
6 changes: 6 additions & 0 deletions internal/translations/testdata/fill-in/ios-swift.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// swiftlint:disable all
import Foundation
struct Translations {
static let SOMETHING = NSLocalizedString("SOMETHING", comment: "")
static let SOMETHING_ONLY_IN_EN = NSLocalizedString("SOMETHING_ONLY_IN_EN", comment: "")
}
4 changes: 4 additions & 0 deletions internal/translations/testdata/fill-in/json-da.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"something": "Noget",
"something_only_in_en": "Something only in English"
}
4 changes: 4 additions & 0 deletions internal/translations/testdata/fill-in/json-en.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"something": "Something",
"something_only_in_en": "Something only in English"
}
8 changes: 6 additions & 2 deletions internal/translations/translations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ func newTranslations(path string) (*translationData, error) {
return &translationData{data: records}, nil
}

func (t *translationData) translation(keyIndex int, valueIndex int) *translation {
func (t *translationData) translation(keyIndex int, valueIndex int, useFallback bool, fallbackIndex int) *translation {
items := map[string]string{}
for _, r := range t.data {
items[strings.ToLower(r[keyIndex-1])] = r[valueIndex-1]
s := r[valueIndex-1]
if useFallback && s == "" {
s = r[fallbackIndex-1]
}
items[strings.ToLower(r[keyIndex-1])] = s
}
return newTranslation(items)
}
2 changes: 1 addition & 1 deletion internal/translations/translations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestTranslationsTranslation(t *testing.T) {
if !assert.NoError(t, err) {
return
}
translation := translations.translation(1, 3)
translation := translations.translation(1, 3, false, 0)

assert.NotNil(t, translation)
}