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

fix: generate i18n_resource binary file for user on rewrite-package #131

Merged
merged 6 commits into from
May 21, 2024

Conversation

Aerex
Copy link
Collaborator

@Aerex Aerex commented Apr 19, 2024

Context

Currently, i18n4go expects the user to generate a binary file of the translations file (using go-bindataa) prior to running the rewrite-package comand. This will lead to runtime errors for the updated source code if the generated go file (i18n_resources.go) is not present in the correct package.

Note: This issue ONLY occurs if the --init-code-snippet-filename is NOT provided

This PR will handle the binary file generation for the user by generating the file and storing in the correct package.

Callouts

  • CF should not be a dependency when creating translations so I removed the expected cf folder when searching for localization file

Steps to Test

Setup

  1. Create a project directory (eg. test)
  2. Create a go.mod: go mod init example.com/test
  3. Copy the examples/demo1.go and examples/excluded.json to the directory created in step 1
  4. Create a new folder called i18n
  5. Copy the [examples/excluded.json](https://github.com/maximilien/i18n4go/blob/fbc2394cd3e8877e4ea823bdb646a1018692a6c8/examples/demo1/excluded.json to the i18n folder in step 3
  6. Run the extract-strings command to extract the strings and generated localization file (demo1.go.en.json):
    - i18n4go extract-strings -d ./ -r -o i18n/resources --exclude i18n/excluded.json
  7. Merge the localization files with the command: i18n4go merge-strings -d i18n/resources -s en
  8. Remove the generated localization file from step 5

Replicate Issue

  1. Install or build i18n4go@master release
  2. Run the command: i18n4go rewrite-package --verbose --directory ./ --i18n-strings-filename i18n/resources/en.all.json
  3. Verify that the i18n_init.go file is generated
  4. Open the i18n_init.go file and verify that the github.com/maximilien/i18n4go/i18n package fails to import
  5. Open the package using your IDE to open the init.go file or open the file directly located ../../go/../pkg/mod/github.com/maximilien/[email protected]/i18n/init.go
  6. Verify that the error: undefined: resources appears on line 117

Resolution

  1. Delete the generated i18n_init.go file
  2. Run the command: go mod edit -replace github.com/maximilien/i18n4go=/root/path/to/i18n4go/source/code
  3. Run go mod tidy
  4. Build the CLI from PR branch
  5. Rerun the command using CLI from PR branch: i18n4go rewrite-package --verbose --directory ./ --i18n-strings-filename i18n/resources/en.all.json
  6. Verify that i18n_resources.go file has been generated
  7. Build the demo1.go: go build -o demo
  8. Run the file ./demo
  9. Verify that translation is printed correctly

Resolves: #127

@Aerex Aerex requested a review from maximilien April 19, 2024 19:48
Copy link
Owner

@maximilien maximilien left a comment

Choose a reason for hiding this comment

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

See comment

i18n4go/cmds/rewrite_package.go Outdated Show resolved Hide resolved
@Aerex Aerex requested a review from maximilien April 26, 2024 21:19
Copy link
Owner

@maximilien maximilien left a comment

Choose a reason for hiding this comment

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

LGTM

@maximilien maximilien merged commit 452ab7f into master May 21, 2024
5 checks passed
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.

Missing placeholder for importing the go-bindata resource method in the template
2 participants