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

32-transform-to-and-from-xliff-2 #33

Merged
merged 5 commits into from
Mar 9, 2023

Conversation

VladislavsPerkanuks
Copy link
Contributor

No description provided.

@VladislavsPerkanuks VladislavsPerkanuks linked an issue Mar 8, 2023 that may be closed by this pull request
@VladislavsPerkanuks VladislavsPerkanuks self-assigned this Mar 8, 2023

type unit struct {
ID string `xml:"id,attr"`
Notes *[]note `xml:"notes>note"` // Set as pointer to avoid empty <notes></notes> when marshalling.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried many solutions, but this is the only one that works: avoiding <notes></notes> when marshaling xliff2 struct with no Notes.
If there is a better way/ways I am happy to hear about them :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try omitempty

Suggested change
Notes *[]note `xml:"notes>note"` // Set as pointer to avoid empty <notes></notes> when marshalling.
Notes []note `xml:"notes>note,omitempty"`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not work, already tried

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en">
  <file>
    <unit id="Welcome">
      <notes>
        <note category="description">To welcome a new visitor</note>
      </notes>
      <segment>
        <source>Welcome to our website!</source>
      </segment>
    </unit>
    <unit id="Error">
      <notes>
        <note category="description">To inform the user of an error</note>
      </notes>
      <segment>
        <source>Something went wrong. Please try again later.</source>
      </segment>
    </unit>
    <unit id="Feedback">
      <notes></notes>
      <segment>
        <source>We appreciate your feedback. Thank you for using our service.</source>
      </segment>
    </unit>
  </file>
</xliff>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not possible using field tag golang/go#7233

It is possible by implementing xml.Marshaler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good enough as it is (*[]note) or []note `xml:"notes>note,omitempty" leaving empty <notes></notes> when there is no description ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*[]note

return nil, fmt.Errorf("marshal model.Messages to XLIFF 2 formatted XML: %w", err)
}

dataWithHeader := append([]byte(xml.Header), data...) // prepend generic XML header
Copy link
Contributor Author

@VladislavsPerkanuks VladislavsPerkanuks Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding XML Declaration is recommended
https://www.w3.org/TR/xml/#sec-prolog-dtd

</unit>
</file>
</xliff>`),
want: model.Messages{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to call it expected?
expected: model.Messages...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you are right, but I guess it should be in a separate PR, as if I change I should change in all files to remain consistency

}{
{
name: "All OK",
data: []byte(`<?xml version="1.0" encoding="UTF-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here maybe input?

}

// Matches a substring that starts with > and ends with < with zero or more whitespace in between.
re := regexp.MustCompile(`>(\s*)<`)
Copy link
Contributor

@janishorsts janishorsts Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create a test helper function if you use it in multiple tests.

func equalXml(t *testing.T, expected, actual []byte) boolean {
  t.Helper()

  // ....
}

@VladislavsPerkanuks VladislavsPerkanuks merged commit 69f6865 into main Mar 9, 2023
@VladislavsPerkanuks VladislavsPerkanuks deleted the 32-transform-to-and-from-xliff-2 branch March 9, 2023 09:19
DarjaLeonova pushed a commit that referenced this pull request Mar 13, 2023
DarjaLeonova added a commit that referenced this pull request Mar 13, 2023
* #19 Transform-to-and-from-POT

* #19 Transform-to-and-from-POT

* #19 Tidy

* #19 minor fixes

* #19 minor fixes

* #19 minor fixes

* #19 fixes

* #19 changed multiline msgdid and msgstr

* #19 modified ToPot function

* #19 fixes

* #19 modified multiline functionality

* #19 added error to lexer function

* #19 added quoted string support in lex function and made fixes

* #19 changed fully Lex function because of gocognit

* #19 changed PoTag -> poTag

* #19 changed identifiers to private

* #19 added comment multiline support

* #19 fixes

* #34 transform to and from XMB

* 17-transform-to-and-from-arb (#20)

* File-upload2 (#13)

* 21-download-file (#22)

* Instrument traces (OpenTelemetry) (#24)

* #27 Upgraded earthly to 0.7 (#28)

* #25 Added configuration from various sources (#29)

* 32-transform-to-and-from-xliff-2 (#33)

* 18-transform-to-and-from-angularlocalize (#31)

* Renamed table test fields for consistency (#38)

---------

Co-authored-by: Vladislavs Perkaņuks <69954124+VladislavsPerkanuks@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Transform to and from XLIFF 2
3 participants