Support system locale detection and standard GNU/Linux translation paths for gettext
| .vscode | ||
| source | ||
| .gitignore | ||
| dub.json | ||
| dub.selections.json | ||
| LICENSE | ||
| README.md | ||
gettext_syslocale
A D module that extends the gettext library to support system locale detection and standard GNU/Linux translation paths.
Overview
The gettext_syslocale module enhances the gettext library by automatically selecting the system locale from environment variables (LC_ALL, LC_MESSAGES, LANG) and loading translation .mo files from standard GNU/Linux paths (/usr/share/locale/<language>/LC_MESSAGES/<programName>.mo). It prioritizes full language codes (e.g., ru_RU) before falling back to short codes (e.g., ru).
Features
- Automatically detects the system locale.
- Searches for
.mofiles in/usr/share/locale/<language>/LC_MESSAGES/<programName>.mo. - Checks full language codes (e.g.,
ru_RU) before short codes (e.g.,ru).
Usage
import gettext;
import gettext_syslocale;
void main(string[] args) {
mixin(gettext.main);
initializeSystemLocale(args[0].baseName);
writeln(tr!"Hello, world!");
}
- Include
mixin(gettext.main)in yourmainfunction. - Call
initializeSystemLocalewith the program name (e.g.,args[0].baseName). - Use
trfor translatable strings, as provided by thegettextlibrary.
Requirements
- D compiler (e.g., DMD, LDC).
- The
gettextlibrary for D, including themofilemodule. .motranslation files in/usr/share/locale/<language>/LC_MESSAGES/.
Installation
Add gettext_syslocale.d to your project and ensure the gettext library is available. Compile with a D compiler supporting the gettext library.
License
Boost License 1.0, consistent with the gettext library.