dngettext - Linux


Overview

dngettext gets the English singular or plural translation for a messageid, depending on the number specified. This command is commonly used in conjunction with the gettext utility for internationalizing applications.

Syntax

dngettext MSGID MSGID_PLURAL N

Parameters

  • MSGID: The singular form of the message.
  • MSGID_PLURAL: The plural form of the message.
  • N: The number to use for determining the singular or plural form.

Options/Flags

  • -c, –context: Specify the context for the message.
  • -n, –number: Same as the positional argument N.

Examples

Singular Translation

dngettext "file" "files" 1

Output:

file

Plural Translation

dngettext "file" "files" 2

Output:

files

Specifying Context

dngettext "File" "Files" 1 --context=Documents

Output:

File (Documents)

Common Issues

Incorrect Plural Form

If the plural form is not provided, dngettext will fall back to the singular form for both singular and plural numbers.

Integration

dngettext is typically used in conjunction with the xgettext and msgfmt utilities for internationalizing applications.

Script Example

#!/bin/bash

# Extract translatable strings from source files
xgettext -o translations.pot source_files.c source_files.h

# Generate message catalog for a specific language code
msgfmt -o translations.es.mo translations.pot

# Use dngettext to get a translated message
echo $(dngettext "File" "Files" 1 --context=Documents)

Related Commands

  • gettext: Translates a message using the specified context.
  • xgettext: Extracts translatable strings from source code.
  • msgfmt: Compiles message catalogs for a specific language.