This project provides a Cloudflare Worker and a Ruby CLI script for translating messages.properties
files into different languages. The primary use case is to assist developers working with internationalization in Java Spring Boot applications by automating the process of translating message files.
The goal of this project is to simplify the process of translating message properties files into multiple languages using a combination of Cloudflare Workers and a Ruby CLI script. This allows for easy and efficient handling of translations directly from your development environment.
The AI model being used is the m2m100, and you can see the languages supported here: https://huggingface.co/facebook/m2m100_1.2B#languages-covered Currently dialects are not supported, such as Brazililan Portugese versus Portugese Portuguse, or the Hong Kong or Tiawanese dialects of Chinese.
To deploy the Cloudflare Worker that handles the translation of messages.properties
files, follow these detailed steps:
If you haven't already, you'll need to install the Wrangler CLI, which is used to manage and deploy Cloudflare Workers.
-
Install Wrangler:
npm install -g wrangler
-
Login to Cloudflare: After installing Wrangler, log in to your Cloudflare account:
wrangler login
This command will open a browser window for you to authenticate your Cloudflare account.
Once everything is set up, you can deploy your Worker to Cloudflare.
wrangler publish
This command will bundle your project and deploy it to Cloudflare Workers. Wrangler will provide you with a URL where your Worker is hosted.
After deploying, you can test your Worker by sending a request to the provided URL. Ensure that your Worker correctly handles the POST
requests, translates the messages file, and returns the translated content.
To interact with your deployed Worker, you can use tools like curl
or the provided Ruby CLI script.
curl -X POST -F "file=@messages.properties" -F "language=fr" https://your-worker-url.workers.dev
This command uploads messages.properties
and requests a translation to French. The Worker responds with a translated file.
-
Deployment Errors: If you encounter issues during deployment, check the output in your terminal for error messages. Common issues include incorrect bindings, missing environment variables, or authentication issues.
-
Log and Debug: Use the
wrangler tail
command to stream logs from your deployed Worker, which can help with debugging:wrangler tail
-
403 Errors: If you configure your Worker to use a Custom Domain, you may encounter 403 errors depending on how Cloudflare is configured to protect that domain. Test with the default Route (which ends in workers.dev) to see if this is the issue.
This project includes a pages
directory containing an index.html
file, which serves as a front-end form for interacting with the Cloudflare Worker. This HTML form allows users to upload a messages.properties
file and specify a target language for translation.
This is completely optional!
Before deploying the pages
directory as a Cloudflare Pages application, you need to update the index.html
file to point the form's action to your Cloudflare Worker URL.
-
Locate the
index.html
File:- The file is located in the
pages
directory:pages/index.html
.
- The file is located in the
-
Edit the Form Action:
-
Open
pages/index.html
in your preferred text editor. -
Find the
<form>
element in the HTML code. It will look something like this:<form action="https://your-worker-url.workers.dev" method="post" enctype="multipart/form-data">
-
Replace
https://your-worker-url.workers.dev
with the actual URL of your deployed Cloudflare Worker. This URL is where the form will submit the uploaded file and target language.
-
-
Save the Changes:
- After updating the form action, save the
index.html
file.
- After updating the form action, save the
Once you have edited the index.html
file, you can deploy the pages
directory as a Cloudflare Pages application.
-
Install Wrangler CLI:
-
If you haven't already, install the Wrangler CLI, which is used to manage Cloudflare Workers and Pages:
npm install -g wrangler
-
-
Login to Cloudflare:
-
Log in to your Cloudflare account using Wrangler:
wrangler login
-
-
Deploy the Pages Application:
-
Navigate to the root of your project directory in your terminal.
-
Use the following command to deploy the
pages
directory:wrangler pages publish pages --project-name <your-project-name>
-
Replace
<your-project-name>
with a unique name for your Cloudflare Pages project.
-
-
Access Your Deployed Pages App:
- Once the deployment is complete, Wrangler will provide you with a URL where your Cloudflare Pages application is hosted. Visit this URL to access the form and use it to upload and translate
messages.properties
files.
- Once the deployment is complete, Wrangler will provide you with a URL where your Cloudflare Pages application is hosted. Visit this URL to access the form and use it to upload and translate
After deploying, your form should be accessible via the URL provided by Cloudflare Pages. Users can visit this URL, upload their messages.properties
file, and receive translated versions directly through the form.
The Ruby CLI script automates the interaction with the Cloudflare Worker, allowing you to upload a messages.properties
file, specify multiple target languages, and download the translated files directly to your local machine.
-
Ensure you have Ruby installed on your system.
-
Install the
multipart-post
gem, which is used for handling file uploads in HTTP requests:gem install multipart-post
If you haven't edited the WORKER_URL in the script itself, you should set an environment variable WORKER_URL pointing to your Worker:
export WORKER_URL="https://yourworker.hostname.com"
The script provides several options to customize its behavior:
-
Default Behavior:
- By default, the script uploads
messages.properties
from the current directory and translates it into the languages specified in theDEFAULT_LANGUAGES
list:
ruby translate_messages.rb
- By default, the script uploads
-
Specify a Custom File:
- You can specify a different file to upload using the
-f
or--file
option:
ruby translate_messages.rb -f custom_messages.properties
- You can specify a different file to upload using the
-
Specify Custom Target Languages:
- You can specify a comma-separated list of target languages using the
-l
or--languages
option. This overrides the default languages:
ruby translate_messages.rb -l fr,es,it
- You can specify a comma-separated list of target languages using the
-
Combine Options:
- You can combine the file and language options to customize both the file to be uploaded and the target languages:
ruby translate_messages.rb -f custom_messages.properties -l fr,es,it
-
Target Languages: You can modify the list of default target languages by editing the
DEFAULT_LANGUAGES
array within the script. This allows you to customize which languages the file will be translated into by default. -
Worker URL: You can also replace the
WORKER_URL
in the script with the URL of your Translate Messages Cloudflare Worker.
When the script runs successfully, you will see messages indicating that the translated files have been saved in the current directory. Each file will have the appropriate language suffix.
- Cloudflare Worker: Handles the actual translation logic.
- Cloudflare Pages HTML: An example HTML page form to upload a message.properties file for translation.
- Ruby CLI Script: Facilitates interaction with the Worker and automates the translation process.
This project provides a streamlined and automated solution for translating Java properties files across multiple languages, using the power of Cloudflare Workers and a simple Ruby CLI. It is ideal for developers working on internationalized applications, providing a seamless workflow for managing translations.
For more details and to view the source code, please refer to the files in this GitHub repository.