Skip to content

Instantly share code, notes, and snippets.

@thecraftman
Last active January 26, 2025 07:09
Show Gist options
  • Save thecraftman/5c8fdd733bdac2bd405e6b195f55090c to your computer and use it in GitHub Desktop.
Save thecraftman/5c8fdd733bdac2bd405e6b195f55090c to your computer and use it in GitHub Desktop.
Integrate Locize CLI with DevOps Practices

Integrate Locize API with Kubernetes and DevOps Practices

The Locize API enables you to integrate your project using the Command Line. The locize Command Line enables you to add new segments or complete files.

locize cli can be used to import / export locales, add / edit / remove sync segments.

How to Install the Locize CLI

Before Installing the locize CLI, make sure NMP is installed. If NPM isn't installed follow the process here to install it.

  • Create the folder for your project.
On your terminal type in these commands; 
mkdir locize-cli
cd locize-cli
npm init 

  • Install the Cli
npm install locize-cli

Locize will be implemented using NPX, NPX is a NPM package runner can be used to run a locally installed package easily. It is also a CLI tool whose purpose is to make it easy to install and manage dependencies hosted in the npm registry.

  • Print the Locize Cli
npx locize -h

OUTPUT:

Usage: locize [options] [command]

Options:
  -V, --version                              output the version number
  -a, --add-path <url>                       Specify the add-path url that should be used (default:
                                             https://api.locize.app/update/{{projectId}}/{{version}}/{{lng}}/{{ns}})
  -h, --help                                 display help for command

Commands:
  migrate|m [options]                        migration of existing translation files
  add|a [options] <namespace> <key> <value>  add a new key
  remove|rm [options] <namespace> <key>      remove a key
  download|dl [options]                      download namespaces
  get|g [options] <namespace> <key>          get a key
  sync|s [options]                           synchronizes locize with your repository (or any other local directory)
  save-missing|sm [options]                  saves missing keys to locize from your repository (or any other local directory)
  copy-version|cv [options] <fromVersion>    copy version
  publish-version|pv [options]               publish version
  delete-namespace|dn [options] <namespace>  delete a namespace
  format|ft [options] [fileOrDirectory]      format local files
  help [command]                             display help for command
  • Print the download command usage
npx locize dl -h

OUTPUT:

Usage: locize download|dl [options]

download namespaces

Options:
  -i, --project-id <projectId>           The project-id that should be used
  -v, --ver <version>                    The version that should be targeted (default: latest)
  -l, --language <lng>                   The language that should be targeted
  -n, --namespace <ns>                   The namespace that should be targeted
  -p, --path <path>                      Specify the path that should be used (default: /home/thecraftman/myproject) (default:
                                         "/home/thecraftman/myproject")
  -g, --get-path <url>                   Specify the get-path url that should be used (default:
                                         https://api.locize.app/{{projectId}}/{{version}}/{{lng}}/{{ns}})
  -k, --api-key <apiKey>                 The api-key that should be used
  -f, --format <json>                    File format of namespaces (default: json; [flat, xliff2, xliff12, xlf2, xlf12, android, yaml,
                                         yaml-rails, yaml-nested, csv, xlsx, po, strings, resx, fluent, tmx, laravel, properties])
                                         (default: "json")
  -s, --skip-empty <true|false>          Skips to download empty files (default: true) (default: "true")
  -P, --language-folder-prefix <prefix>  This will be added as a local folder name prefix in front of the language. (default: "")
  -m, --path-mask <mask>                 This will define the folder and file structure; do not add a file extension (default:
                                         {{language}}/{{namespace}}) (default: "{{language}}/{{namespace}}")
  -c, --clean <true|false>               Removes all local files by removing the whole folder (default: false) (default: "false")
  -C, --config-path <configPath>         Specify the path to the optional locize config file (default:
                                         /home/thecraftman/myproject/.locize or /home/thecraftman/.locize)
  -h, --help                             display help for command
  Examples:

    $ locize download
    $ locize download --ver latest
    $ locize download --project-id <projectId> --ver latest --language en --namespace common
    $ locize download --project-id <projectId> --ver latest --language en --namespace common --format flat
    

The locize download command shortcuts can be used for typing the locize commands in the Command line.

  • Adding/Updating new keys
npx locize add -k (paste your api key here) -i (paste your project Id here) -l en namespace1 myNewKey "My new value"

  • Download Locize Project
 npx locize dl -p ./translation -k (api key) -i (project id)
  • Add a new key and value
Enter the add command to see the right method to add the key and value using (npx locize add -h).


  • Use the add command
 npx locize add -k (api key here) -i (project id here) -l de translation "(key here)" "(value here)"
  • Download the key and value you just added using the download command
 npx locize dl -p ./translation -k (api key) -i (project id)
  • Synchronize locize with your repository
 npx locize sync  -k (api key here) -i (project id here)

Confirm changes on your Locize Webpage.


Deploying Locize API using Kubernetes

apiVersion: v1
kind: ConfigMap
metadata:
  name: locize-config
data:
  LOCIZE_SYNC_PATH: "/app/translations"
  LOCIZE_FORMAT: "json"
  LOCIZE_PATH_MASK: "{{language}}/{{namespace}}"
---
apiVersion: v1
kind: Secret
metadata:
  name: locize-secrets
type: Opaque
data:
  LOCIZE_API_KEY: "api_key_here"
  LOCIZE_PROJECT_ID: "project_id_here"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: locize-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: locize-app
  template:
    metadata:
      labels:
        app: locize-app
    spec:
      initContainers:
      - name: locize-sync
        image: node:16-alpine
        command:
        - /bin/sh
        - -c
        - |
          npm install -g locize-cli
          npx locize download \
            -k $(LOCIZE_API_KEY) \
            -i $(LOCIZE_PROJECT_ID) \
            -p $(LOCIZE_SYNC_PATH) \
            -f $(LOCIZE_FORMAT) \
            -m $(LOCIZE_PATH_MASK)
        envFrom:
        - configMapRef:
            name: locize-config
        - secretRef:
            name: locize-secrets
        volumeMounts:
        - name: translations
          mountPath: /app/translations
      containers:
      - name: main-app
        image: your-app-image:tag
        volumeMounts:
        - name: translations
          mountPath: /app/translations
          readOnly: true
        env:
        - name: NODE_ENV
          value: "production"
      volumes:
      - name: translations
        emptyDir: {}
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: locize-sync-job
spec:
  schedule: "0 */6 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: sync
            image: node:16-alpine
            command:
            - /bin/sh
            - -c
            - |
              npm install -g locize-cli
              npx locize sync \
                -k $(LOCIZE_API_KEY) \
                -i $(LOCIZE_PROJECT_ID) \
                -p $(LOCIZE_SYNC_PATH)
            envFrom:
            - configMapRef:
                name: locize-config
            - secretRef:
                name: locize-secrets
            volumeMounts:
            - name: translations
              mountPath: /app/translations
          volumes:
          - name: translations
            emptyDir: {}
          restartPolicy: OnFailure
@thecraftman
Copy link
Author

Deploy your Locize configuration on a DigitalOcean droplet

First, create a new droplet:

  • Choose Ubuntu as the operating system
  • Select a plan with at least 2GB RAM
  • Choose your preferred region
  • Add your SSH key for secure access
#!/bin/bash

# Update system packages
apt-get update && apt-get upgrade -y

# Install prerequisites
apt-get install -y apt-transport-https ca-certificates curl software-properties-common

# Install Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io

# Start and enable Docker
systemctl start docker
systemctl enable docker

# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv kubectl /usr/local/bin/

# Install Minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
install minikube-linux-amd64 /usr/local/bin/minikube
rm minikube-linux-amd64

# Start Minikube
minikube start --driver=docker

# Create necessary directories
mkdir -p ~/locize-deployment
  • Connect to your droplet
ssh root@your_droplet_ip
  • Save the setup script and run the command in your terminal:
chmod +x setup.sh
./setup.sh
  • Create your Kubernetes configuration file:
cd ~/locize-deployment
nano locize-k8s.yaml

Verify all your deployments and apply it:

# Check all resources
kubectl get all

# Check pods status
kubectl get pods

# Check logs of the init container
kubectl logs <pod-name> -c locize-sync

# Monitor the CronJob
kubectl get cronjobs
kubectl get jobs

Storage: The current configuration uses emptyDir volumes, which are temporary. For production, you might want to use persistent volumes:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: locize-translations-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/translations"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: locize-translations-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Monitoring: You can set up monitoring using Prometheus and Grafana:

# Install Helm first
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# Add Prometheus repository
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

# Install Prometheus stack
helm install monitoring prometheus-community/kube-prometheus-stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment