app.i18n - Internationalization API

Multi-language support and localization.

Methods

app.i18n.t(key, args?, default?)

Gets translated text.

Parameters:

  • key (string) - Translation key
  • args (array, optional) - Interpolation parameter array
  • default (string, optional) - Default value (returned when the key does not exist)

Returns: string - Translated text

-- Simple translation
local text = app.i18n.t("menu.title")

-- With parameters (parameters replace {1}, {2}, etc. in order)
local text = app.i18n.t("file.count", {5})
-- In the translation file: "file.count": "{1} files in total"
-- Result: "5 files in total"

-- With default value
local text = app.i18n.t("missing.key", nil, "Default text")

app.i18n.locale()

Gets the current locale.

Returns: string - Language code

local locale = app.i18n.locale()  -- "zh-Hans" or "en"

app.i18n.has(key)

Checks whether a translation key exists.

Parameters:

  • key (string) - Translation key

Returns: boolean

if app.i18n.has("custom.message") then
    local text = app.i18n.t("custom.message")
end

app.i18n.locales()

Gets the list of languages supported by the plugin.

Returns: array - Array of language codes

local locales = app.i18n.locales()
-- {"zh-Hans", "en"}

Translation File Format

Translation files are located in the locales/ subdirectory of the plugin directory, using JSON format:

locales/zh-Hans.json:

{
    "plugin.name": "Image Tools",
    "menu.compress": "Compress Images",
    "message.processing": "Processing {1}/{2}...",
    "message.complete": "Processed {1} files"
}

locales/en.json:

{
    "plugin.name": "Image Tools",
    "menu.compress": "Compress Images",
    "message.processing": "Processing {1}/{2}...",
    "message.complete": "Processed {1} files"
}

Examples

Plugin with Internationalization

function MyPlugin:handleCompress(context)
    local files = context.selectedFiles

    if #files == 0 then
        app.notification.show(
            app.i18n.t("plugin.name"),
            app.i18n.t("message.noFiles", nil, "Please select files")
        )
        return
    end

    for i, file in ipairs(files) do
        app.log.info(app.i18n.t("message.processing", {i, #files}))
        -- Process file...
    end

    app.notification.show(
        app.i18n.t("plugin.name"),
        app.i18n.t("message.complete", {#files})
    )
end
Developer Documentation
User Guide
Getting Started Script Menus FAQ
Script Development
Development Guide
Plugin Development
Quick Start Development Guide Example Plugins
API Reference
Overview API Query Plugin Info Logging Finder Context Plugin Settings Internationalization
UI & Interaction
Dialog Progress Notification Chooser WebView Status Bar Dock
Files & Paths
File Operations Path Utilities Finder Actions Trash Extended Attributes Metadata File Watcher
Data Formats
JSON Plist CSV XML PDF Image
Text & Encoding
String Regex Date & Time Color Crypto
System
Shell Commands Process Application System Info AppleScript Shortcuts
System Info
Network Power/Battery Screen/Appearance Audio Bluetooth Location
Network
HTTP WebSocket URL
Input & Clipboard
Keyboard Mouse Hotkey Clipboard Window
Storage
SQLite Keychain UserDefaults
Media
OCR QR Code
Utilities
Archive UTI Share Timer Wake Lock Thread