Utility to convert csv data into android, ios or web string resource formats.
A lot of the times we build applications that spans across countries and regions, and we want to add local languages (i.e localization) translation in our application. Nowadays, there are tools to manage localization, which easily integrate with CI CD pipelines to automatically update content, even so they sometimes provide resource files for platform specific format. Sometimes, they dont, and one of the most common formats you can get data is csv. Hence, this tool is written focusing csv file as input, which is described in detail later. This tool can be used to automate updating the string resources from same source, without manually updating the file each time.
Run this one-liner to automatically install the localize command:
curl -fsSL https://raw.githubusercontent.com/talhahasanzia/string-resource-utility/main/install.sh | bashOr if you prefer wget:
wget -qO- https://raw.githubusercontent.com/talhahasanzia/string-resource-utility/main/install.sh | bashThis will:
- Clone the repository to a temporary location
- Build the executable for macOS
- Install it to
~/.local/bin/localize - Add
~/.local/binto your PATH - Clean up temporary files
After installation, restart your terminal or run source ~/.zshrc (or your shell profile) to use the localize command globally.
- Install Go.
- Goto working directory.
- To run the utility
go run localize.go -file=strings.csv -platform=ios - To build an executable,
go build localizeand you can redistribute.
- Prepare a csv file with 1st column as key, and subsequent columns as locales e.g. Note: key and locale will be trimmed.
| key | en | es | it |
|---|---|---|---|
| welcome_text | welcome | bienvenidos | benvenuto |
| bye_text | goodbye | adiós | arrivederci |
- run
./localize -file=<PATH_TO_CSV> -platform=<IOS_ANDROID_WEB> -debug=<TRUE_FALSE> -file: Absolute path to csv file-output: output directory for generated files-overwrite: overwrite existing file contents: true/false-platform: platform for which string resource will be generated. Options:ios,androidandweb(anyone at a time).-debug: Show logs while creating resources, by default it isfalse. Options:trueorfalse- Currently, it will generate resource files in the same folder where the
localizecommand is executed.
If you chose -platfrom=android, the generated files will be strings.xml, since we are targeting multiple locales, this tool will generate strings_en.xml for en locale, and so on. Here is what this generated file looks like when above mentioned csv is used:
<resources>
<string name="welcome_text">welcome</string>
<string name="bye_text">goodbye</string>
</resources>
Specifically for android, you have to specify keys without spaces.
Similarly, if you chose -platfrom=ios, the generated files will be Localized.strings, since we are targeting multiple locales, this tool will generate Localized_en.strings for en locale, and so on. Here is what this generated file looks like when above mentioned csv is used:
"welcome_text" = "welcome";
"bye_text" = "goodbye";
Similarly, if you chose -platfrom=flutter, the generated files will be app_<en>.arb, since we are targeting multiple locales, this tool will generate app_en.arb for en locale, and so on. Here is what this generated file looks like when above mentioned csv is used:
{
"welcome_text": "welcome",
"bye_text": "goodbye",
"eof":"eof"
}
Note that a dummy entry as "eof" is set in the end of arb file in order to mitigate error resulting from last left comma (,) when flutter gen-l10n is run.
And finally, if you chose -platfrom=web (assuming you are working on a typescript framework), the generated files will be strings.ts, since we are targeting multiple locales, this tool will generate strings_en.ts for en locale, and so on. Here is what this generated file looks like when above mentioned csv is used:
const LOCALIZED_STRINGS = {
welcome_text: "welcome",
bye_text: "goodbye",
}
export default LOCALIZED_STRINGS;
You can always customize the way this tool work, following are the files where you will find relevant logic:
reader.go: Reads data from.csvfile and returns a 2d array.parser.go: Gets 2d array data as input and returns a list ofRecordobjectwriter.go: Writes to file for eachRecordentry, and also responsible for opening and closing data specific to platform.
- Once the data is ready to be written in the form of list (slice) of
Record, the code groups records by locale. - Each locale's records are processed sequentially, writing all entries for one locale before moving to the next.
- For each locale, the code creates platform + locale specific files and populates some pretext if needed by that specific platform.
- The writer processes all records for the current locale in a simple loop, writing each entry to the appropriate file.
- This is achieved by grouping records using a map where locale is the key and a slice of records is the value. So
localeRecords[locale]contains all entries for that locale. - After all records for a locale are written,
CloseFileis called which writes the closing statement (if needed by platform) to the platform + locale specific file. - This sequential approach is simpler to understand and debug while producing the same output as the previous concurrent implementation.
- Sequential approach also fixes an issue when sometimes, last entry is placed outside closing token.
Find releases for your specific platforms in releases section.
.zip file contains executables for 64-bit (marked as amd64) and 32-bit (marked as 386) versions. arm is target release for Apple M1 Macbooks. .exe for Microsoft Windows.
Feel free to create PRs and report issues or submit feedback. Just remember to be polite and respectful :)
