This is a simple command-line program written in Go that uses the Google Cloud Translation API to translate i18n json with text from one language to another. The program takes in a JSON file containing text to be translated, source language, target language and output JSON file path as required command-line arguments.
To use the program, you'll need to have a Google Cloud service account and API key. You can create one by following the instructions in the Google Cloud documentation.
Once you have your credentials, you can run the program using the following command:
export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"
i18n-gt -input <inputfile> -source <source language> -target <target language> -output <outputfile>
where <inputfile>
is the path to the input JSON file, <source language>
is the code for the source language (e.g. "en" for English), <target language>
is the code for the target language, <outputfile>
is the path to the output JSON file
The input JSON file should contain an object, with property and value
Here's an example input JSON file:
{
"title": "Welcome!",
"pages": {
"title": "Pages",
"profile": {
"title": "Profile page",
"login": {
"pasword": "password"
}
}
}
}
The output JSON file will contain translated object
Here's an example output JSON file:
{
"pages": {
"profile": {
"login": {
"pasword": "hasło"
},
"title": "Strona profilowa"
},
"title": "Strony"
},
"title": "Powitanie!"
}
The program uses the following Go packages:
cloud.google.com/go/translate
: The Google Cloud Translation API Go client librarygolang.org/x/text/language
: The Go standard library package for language code parsinggoogle.golang.org/api/option
: The Google Cloud API option library
You can install these packages using the go get
command:
go get cloud.google.com/go/translate
go get golang.org/x/text/language
go get google.golang.org/api/option
To build the program for multiple platforms, run the following command:
make build
This will compile the program and generate a binary file in the build/
directory.
To run the program in development mode, run the following command:
make dev
This will run the program with the input.json
and account.json
files provided and generate the output.json
file.
The Makefile
provides the following build targets:
build-linux-amd64
: Build the program for Linux x64build-linux-arm
: Build the program for Linux ARMbuild-darwin-amd64
: Build the program for macOS x64build-windows-amd64
: Build the program for Windows x64build
: Build the program for all platformsclean
: Clean up generated binary files
Here's an example of how to build the program for a specific platform:
make build-linux-amd64
This will compile the program for Linux x64 and generate a binary file in the build/
directory.
- Set up GitHub release workflow to automate binary file creation and release management.
This program is licensed under the MIT license. See the LICENSE file for details.