Skip to content

Commit

Permalink
✨ Add Localize exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ewen-lbh committed Apr 14, 2024
1 parent 0f130d3 commit 888a9c8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Git exporter: clones a repo, adds and commits the database json file and pushes
- Cloud exporter: uses rclone to upload the database.json file to many cloud services
- Requires key in exporter manifests to specify programs required to run the exporter
- localize exporter: export the database as a single-language database for every language in the original database

### Changed

Expand Down
73 changes: 73 additions & 0 deletions exporter_localize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ortfodb

import (
"fmt"
"os"
"strings"
"text/template"

jsoniter "github.com/json-iterator/go"
"github.com/mitchellh/mapstructure"
)

type LocalizeExporter struct {
}

type LocalizeExporterOptions struct {
FilenameTemplate string `yaml:"filename_template"`
}

func (e *LocalizeExporter) OptionsType() any {
return LocalizeExporterOptions{}
}

func (e *LocalizeExporter) Name() string {
return "localize"
}

func (e *LocalizeExporter) Description() string {
return "Export separately the database as a single database for each language. The `content` field of each work is localized, meaning it's not an object mapping languages to localized content, but the content directly, in the language."
}

func (e *LocalizeExporter) Before(ctx *RunContext, opts ExporterOptions) error {
return nil
}

func (e *LocalizeExporter) Export(ctx *RunContext, opts ExporterOptions, work *AnalyzedWork) error {
return nil
}

func (e *LocalizeExporter) After(ctx *RunContext, opts ExporterOptions, db *Database) error {
options := GetExporterOptions[LocalizeExporterOptions](e, opts)
outputFilenameTemplate, err := template.New("filename").Parse(options.FilenameTemplate)
if err != nil {
return fmt.Errorf("while parsing output filename template %q: %w", options.FilenameTemplate, err)
}

for _, lang := range db.Languages() {
out := make(map[string]map[string]any)
for id, work := range db.Works() {
localizedWork := make(map[string]any)
mapstructure.Decode(work, localizedWork)
localizedWork["content"] = work.Content.Localize(lang)
out[id] = localizedWork
}
var outputFilename strings.Builder
err := outputFilenameTemplate.Execute(&outputFilename, map[string]any{"Lang": lang})
if err != nil {
return fmt.Errorf("while computing output database filename template for language %q: %w", lang, err)
} else if outputFilename.Len() == 0 {
ExporterLogCustom(e, "Warning", "yellow", "output database filename for language %q is empty, skipping", lang)
continue
}

jsonDatabase, err := jsoniter.ConfigFastest.MarshalIndent(out, "", " ")
if err != nil {
return fmt.Errorf("while marshaling localized database to JSON for language %q: %w", lang, err)
}

os.WriteFile(outputFilename.String(), jsonDatabase, 0644)
ExporterLogCustom(e, "Localized", "green", "database in %s to %s", lang, outputFilename.String())
}
return nil
}
5 changes: 1 addition & 4 deletions exporter_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"os"
"strings"

"github.com/mitchellh/mapstructure"
)

type SqlExporterOptions struct {
Expand Down Expand Up @@ -46,8 +44,7 @@ func (e *SqlExporter) Before(ctx *RunContext, opts ExporterOptions) error {
}

func (e *SqlExporter) Export(ctx *RunContext, opts ExporterOptions, work *AnalyzedWork) error {
options := SqlExporterOptions{}
mapstructure.Decode(opts, &options)
options := GetExporterOptions[SqlExporterOptions](e, opts)

_, summary := work.FirstParagraph(options.Language)
e.result += fmt.Sprintf(
Expand Down
30 changes: 29 additions & 1 deletion exporters.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"
"strings"

"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -73,9 +74,9 @@ func ValidateExporterOptions(exporter Exporter, opts ExporterOptions) error {
return nil
}


var BuiltinExporters = []Exporter{
&SqlExporter{},
&LocalizeExporter{},
&CustomExporter{},
}

Expand Down Expand Up @@ -167,3 +168,30 @@ func (ctx *RunContext) DownloadExporter(name string, url string, config map[stri
exporter.name = name
return exporter, nil
}

// GetExporterOptions returns the options for the given exporter.
// Use it to get your options in a nice struct. The struct will be of the same type as the one returned by e.OptionsType().
// Example:
//
// type MyExporterOptions struct {
// // Some option
// Option string `yaml:"option"`
// }
//
// func (e *MyExporter) OptionsType() any {
// return MyExporterOptions{}
// }
//
// func (e *MyExporter) After(ctx *ortfodb.RunContext, opts *ortfodb.ExporterOptions, db *ortfodb.Database) error {
// options := GetExporterOptions[MyExporterOptions](e, opts)
// // Now you can use options as a MyExporterOptions struct
// }
func GetExporterOptions[ConcreteOptionsType any](e Exporter, opts ExporterOptions) ConcreteOptionsType {
options := e.OptionsType()
decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &options,
TagName: "yaml",
})
decoder.Decode(opts)
return options.(ConcreteOptionsType)
}

0 comments on commit 888a9c8

Please sign in to comment.