Skip to content

localization of opencli scripts output #20

@stefanpejcic

Description

@stefanpejcic

Currently we hardcode all messages in EN

but we could pass user's locale from openpanel ui back to opencli

new helper script:

#!/bin/bash

# Usage: translate <msgid> [locale]
# Defaults to $LANG if locale not provided
translate() {
  local msgid=$1
  local locale=${2:-${LANG%%.*}}
  local po_file="translations/$locale/LC_MESSAGES/messages.po"

  if [[ ! -f "$po_file" ]]; then
    echo "Translation file not found for locale $locale" >&2
    return 1
  fi

  local msgstr=$(awk -v id="msgid \"$msgid\"" '
    $0 == id {getline; if ($1 == "msgstr") {
      gsub(/^msgstr\s+"|"\s*$/, "", $0)
      print $0
      exit
    }}
  ' "$po_file")

  if [[ -z "$msgstr" ]]; then
    echo "$msgid"  # fallback to msgid if no translation found
  else
    echo "$msgstr"
  fi
}

and in existing scripts do something like:

source ./translations.sh

# LANG=fr

apply_note=$(translate "Note: Please stop and start the service to apply changes." "$LANG")

message+="<br>${apply_note}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions