lokalise-cli-2-go
lokalise-cli-2-go copied to clipboard
Add support for an `--exclude-langs` option for file download
would be really helpful to have this option, as in my situation I would like to download only the languages I don't maintain myself, for example English and French.
so I'd use it as --exclude-langs=en,fr
.
as of now, I have to update the command on my CI each time I add a new language.
there is a parameter --filter-langs
but it is not clear how to use it. There is no mentioning of this parameter in lokalise2 --help.
For instance when you put --filter-lang en, fr
it only fetches the chages for English. Could someone from support help on this?
@nickustinov @andrewLab-lokalise @yarlson
@christianYoopies --filter-lang
is documented here
@ChristRm the argument is a comma separated list, with just comma, so you should use --filter-lang en,fr
or --filter-lang=en,fr
(no space before fr
)
But let's keep the discussion here about the support for a new --exclude-langs
option 🙂
@yguedidi
While the feature to exclude languages directly with an --exclude-langs
option is not currently available in Lokalise, I've created a workaround script that you might find helpful for your CI setup. This script allows you to dynamically exclude specific languages when downloading files, avoiding the need to update your command each time a new language is added.
Prerequisites
Before using the script, ensure that jq
is installed on your system. jq
is a lightweight and flexible command-line JSON processor.
-
For macOS: You can install
jq
using Homebrew with the commandbrew install jq
. -
For Linux: Installation commands vary by distribution, but for Ubuntu/Debian-based systems, you can use
sudo apt-get install jq
.
Script Overview
The script operates in a few key steps:
- Define Exclusions: Specify the languages you wish to exclude in a comma-separated string.
- Fetch Language List: Use the Lokalise API to get the full list of languages for your project.
- Filter Languages: Dynamically filter out the specified languages from the list.
-
Download Files: Use the filtered list of languages to download files with the
lokalise2 file download
command.
The Script
#!/bin/bash
# Configuration: Replace with your project's ID and Lokalise API token.
PROJECT_ID="your_lokalise_project_id_here"
LOKALISE_TOKEN="your_lokalise_token_here"
EXCLUDE_LANGS="en,fr"
# Convert the excluded languages into a jq filter format.
EXCLUDE_FILTER=$(echo $EXCLUDE_LANGS | awk -F, '{for(i=1; i<=NF; i++) print "\""$i"\"";}' | paste -sd "," -)
# Fetch the language list, excluding specified languages, and format the result as a comma-separated string.
FILTERED_LANGS=$(lokalise2 language list --project-id=$PROJECT_ID --token=$LOKALISE_TOKEN | jq -r --argjson exclude "[$EXCLUDE_FILTER]" '[.languages[].lang_iso | select(. as $lang | $exclude | index($lang) | not)] | join(",")')
# Use the filtered languages in the download command.
lokalise2 file download --project-id=$PROJECT_ID --token=$LOKALISE_TOKEN --filter-langs="$FILTERED_LANGS" --format=json
How It Works
- Set Up Variables: The script starts by defining variables for your project ID, Lokalise token, and the languages you wish to exclude.
-
Create a jq Filter: It then converts the list of languages to exclude into a format that
jq
can use to filter out these languages from the Lokalise language list. -
Fetch and Filter Languages: The script fetches the full list of languages for your project from Lokalise and filters out the excluded languages, using
jq
. -
Download with Filtered Languages: Finally, it uses the filtered list of languages in the
lokalise2 file download
command to download only the files for languages you maintain.
Usage
- Ensure
jq
is installed on your system following the guide above. - Replace
"your_lokalise_token_here"
with your actual Lokalise API token. - Replace
"your_lokalise_project_id_here"
with your actual Lokalise project ID. - Update the
EXCLUDE_LANGS
variable as needed. - Run the script in your CI environment or locally to download the desired language files.
This script should save you time and reduce the manual effort of updating your CI scripts whenever you add new languages to your Lokalise project. It effectively simulates an --exclude-langs
option until such a feature might be directly supported by Lokalise.
@yarlson good idea, thanks!
@yarlson such logic couldn't be implemented directly inside the CLI to add the support for the new option? 😁