Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 70 additions & 26 deletions bin/omarchy-i18n
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,79 @@ english_catalog=$i18n/en/cli.json
registry=$i18n/locales.json
[[ -f $registry ]] || registry=/dev/null

# One jq run picks the string and localizes the arguments; the results come
# back NUL-separated so a string may itself span lines. The -- after --args
# keeps an argument that starts with a dash (a label like "-work") from being
# read as a jq option.
parts=()
mapfile -d '' parts < <(jq -j -n -L "$i18n" \
--arg key "$key" --arg language "$language" --argjson count "$count" \
--slurpfile lang "$language_catalog" --slurpfile en "$english_catalog" --slurpfile registry "$registry" '
include "plural";
def present: . != null and . != "";
($lang[0] // {}) as $l | ($en[0] // {}) as $e | ($registry[0][$language] // {}) as $r
| (if ($l[$key] | present) then [$l[$key], $language]
elif ($e[$key] | present) then [$e[$key], "en"]
else [$key, "en"] end) as [$entry, $from]
| (if $count == null then (if ($entry | type) == "object" then $entry.other else $entry end)
else ($entry | plural_pick($from; $count)) end) as $string
| [$string // $key] + ($ARGS.positional | map(localize_number($r.decimal // "."; $r.zeroDigit // "0")))
| map(tostring + "\u0000") | add
' --args -- "$@" 2>/dev/null) || true
# Fast path for plain, non-plural catalog entries without numeric arguments or
# JSON escapes: avoids spawning jq and parsing three JSON files on every lookup.
# Any key carrying escapes, an empty string, a plural object, or numeric args
# falls through to the jq path below.
fast_lookup() {
local catalog="$1" needle="\"$key\":" line
[[ -f $catalog ]] || return 1
while IFS= read -r line; do
[[ $line == *$needle* ]] || continue
if [[ $line =~ ^[[:space:]]*\"([^\"]+)\"[[:space:]]*:[[:space:]]*\"([^\"\\]+)\"[[:space:]]*,?[[:space:]]*$ ]] && [[ ${BASH_REMATCH[1]} == $key ]]; then
string=${BASH_REMATCH[2]}
return 0
fi
return 2
done <"$catalog"
return 1
}

if (( ${#parts[@]} == 0 )); then
parts=("$key" "$@")
fast_hit=0
if [[ $count == "null" && $key =~ ^[A-Za-z0-9._-]+$ ]] && (( number_only == 0 )); then
has_number=0
for arg in "$@"; do
if [[ $arg =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then
has_number=1
break
fi
done
if (( has_number == 0 )); then
lookup_status=0
fast_lookup "$language_catalog" || lookup_status=$?
if (( lookup_status == 0 )); then
args=("$@")
fast_hit=1
elif (( lookup_status == 1 )) && [[ $language != "en" ]]; then
if fast_lookup "$english_catalog"; then
args=("$@")
fast_hit=1
fi
fi
fi
fi
if (( number_only )); then
printf '%s' "${parts[1]}"
exit 0

if (( !fast_hit )); then
# One jq run picks the string and localizes the arguments; the results come
# back NUL-separated so a string may itself span lines. The -- after --args
# keeps an argument that starts with a dash (a label like "-work") from being
# read as a jq option.
parts=()
mapfile -d '' parts < <(jq -j -n -L "$i18n" \
--arg key "$key" --arg language "$language" --argjson count "$count" \
--slurpfile lang "$language_catalog" --slurpfile en "$english_catalog" --slurpfile registry "$registry" '
include "plural";
def present: . != null and . != "";
($lang[0] // {}) as $l | ($en[0] // {}) as $e | ($registry[0][$language] // {}) as $r
| (if ($l[$key] | present) then [$l[$key], $language]
elif ($e[$key] | present) then [$e[$key], "en"]
else [$key, "en"] end) as [$entry, $from]
| (if $count == null then (if ($entry | type) == "object" then $entry.other else $entry end)
else ($entry | plural_pick($from; $count)) end) as $string
| [$string // $key] + ($ARGS.positional | map(localize_number($r.decimal // "."; $r.zeroDigit // "0")))
| map(tostring + "\u0000") | add
' --args -- "$@" 2>/dev/null) || true

if (( ${#parts[@]} == 0 )); then
parts=("$key" "$@")
fi
if (( number_only )); then
printf '%s' "${parts[1]}"
exit 0
fi
string=${parts[0]}
args=("${parts[@]:1}")
fi
string=${parts[0]}
args=("${parts[@]:1}")

# Substitute through sentinels so an argument that itself contains a %N
# sequence is never substituted twice.
Expand Down
6 changes: 2 additions & 4 deletions bin/omarchy-notification-time
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# LC_TIME, which only catches up with it at the next login. One timestamp feeds
# every part, so a notification sent at midnight can't mix two days.
locale=$(omarchy-language locale)
now=$(date +%s)
day_time=$(LC_TIME=$locale date -d "@$now" +"%A %H:%M")
day_month_year=$(LC_TIME=$locale date -d "@$now" +"%d %B %Y")
IFS=$'\t' read -r day_time day_month_year week < <(LC_TIME=$locale date +"%A %H:%M%t%d %B %Y%t%V")

omarchy-notification-send -g  -u low "$(omarchy-i18n time.now "$day_time" "$day_month_year" "$(date -d "@$now" +%V)")"
omarchy-notification-send -g  -u low "$(omarchy-i18n time.now "$day_time" "$day_month_year" "$week")"