From bae6c26fbf85ef95d24911ab1a114437eac30e31 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 24 Mar 2025 21:08:30 +0300 Subject: [PATCH] init --- .gitignore | 21 ++ LICENSE | 339 ++++++++++++++++++++++++++++++ README.md | 184 ++++++++++++++++ dub.json | 40 ++++ dub.selections.json | 5 + preload/build.sh | 17 ++ preload/override_bindtextdomain.c | 26 +++ scripts/compile_mo.sh | 87 ++++++++ scripts/generate_translations.sh | 140 ++++++++++++ source/singlang.d | 147 +++++++++++++ tests/main.d | 27 +++ tests/shopping.d | 37 ++++ translations/en.po | 50 +++++ translations/ru.po | 51 +++++ 14 files changed, 1171 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 dub.json create mode 100644 dub.selections.json create mode 100755 preload/build.sh create mode 100644 preload/override_bindtextdomain.c create mode 100755 scripts/compile_mo.sh create mode 100755 scripts/generate_translations.sh create mode 100644 source/singlang.d create mode 100644 tests/main.d create mode 100644 tests/shopping.d create mode 100644 translations/en.po create mode 100644 translations/ru.po diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b2cea0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +.dub +docs.json +__dummy.html +docs/ +/singlang +singlang.so +singlang.dylib +singlang.dll +singlang.a +singlang.lib +singlang-test-* +*.exe +*.pdb +*.o +*.obj +*.lst +build/ +messages.pot +.preload/ +run.sh +.vscode/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/LICENSE @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..af50308 --- /dev/null +++ b/README.md @@ -0,0 +1,184 @@ +# Singlang - Lightweight Internationalization (i18n) Library for D + +## Description + +`singlang` integrates GNU Gettext for translations. The module features a singleton `Singlang` class for configuration and a convenient `_()` function for translating messages. + +## Installation + +To use `singlang` in your D project, add it as a dependency in your `dub.json` or `dub.sdl` file: + +### dub.json +```json +{ + "dependencies": { + "singlang": "~>0.1.0" + } +} +``` + +## Usage + +### Initializing the Translation System + +Before translating messages, you must initialize the `Singlang` class with a domain name and a directory containing translation files (`.mo`): + +```d +import singlang; + +void main() { + if (Singlang.set("myapp", "/usr/local/share/locale")) { + writeln("Translation system ready!"); + } else { + writeln("Setup failed, check stderr for details!"); + } +} +``` + +- `domain`: The name of your application or translation domain. +- `localeDir`: The path to the directory containing your `.mo` translation files. + +### Translating Messages + +Use the `_()` function to translate messages: + +```d +writeln(_("Hello, world!")); // Outputs the translated version of "Hello, world!" if available +writeln(_("Error: File not found")); // Outputs translated text or original if unavailable +``` + +If the system is not initialized or a translation is unavailable, the original message is returned. + +## Localization and Build Mechanisms + +This project includes mechanisms to customize localization environments and automate translation workflows, located in the `preload` and `scripts` directories. Below is a detailed overview of their structure, purpose, and how to leverage them effectively in your project. + +### `preload` Directory + +The `preload` directory provides tools to intercept and customize the `bindtextdomain` function from the GNU Gettext library, allowing you to redirect the path to localization files. + +#### Structure + +``` +preload +├── build.sh +└── override_bindtextdomain.c +``` + +#### Files + +##### `build.sh` + +- **Purpose**: Compiles a shared library, `liboverride_bindtextdomain.so`, which intercepts the `bindtextdomain` function to customize localization paths. +- **Usage**: + ```bash + ./preload/build.sh + ``` + Replace `` with the name of your program (e.g., `myapp`). +- **How It Works**: + Compiles `override_bindtextdomain.c` into a shared library, `.preload/liboverride_bindtextdomain.so`, defining `PNAME` as a macro. +- **Output**: Generates `.preload/liboverride_bindtextdomain.so`, which can be preloaded with `LD_PRELOAD` to override `bindtextdomain` behavior. + +##### `override_bindtextdomain.c` + +- **Purpose**: A C source file that intercepts calls to `bindtextdomain` and redirects the localization path to `build/usr/share/locale` for a specified program name (`PNAME`). +- **How It Works**: Checks if the `domainname` matches the predefined `PNAME` (set during compilation). If matched, redirects the localization path to `build/usr/share/locale`. +- **Requirements**: The `PNAME` macro must be defined during compilation (via `build.sh`); if undefined, it exits with an error. +- **Use Case**: Ideal for projects needing to override default localization paths without modifying the core application code. + +### `scripts` Directory + +The `scripts` directory contains utilities to streamline translation file management, including compiling `.mo` files and generating/updating `.po` files. + +#### Structure + +``` +scripts +├── compile_mo.sh +└── generate_translations.sh +``` + +#### Files + +##### `generate_translations.sh` + +- **Purpose**: Automates the creation and updating of `.po` translation files based on D source code in the `source/` directory. +- **Usage**: + ```bash + ./scripts/generate_translations.sh [language] + ``` + Replace `[language]` with an optional language code (e.g., `ru` or `en`). If omitted, processes `ru` and `en` by default. For help: + ```bash + ./scripts/generate_translations.sh --help + ``` +- **How It Works**: + 1. Checks for `.d` files in `source/`. + 2. Generates or updates `translations/messages.pot` using `xgettext` to extract translatable strings (marked with `_`). + 3. For each language: + - Creates a new `.po` file with `msginit` if it doesn’t exist. + - Updates existing `.po` files with `msgmerge`, preserving prior translations. + 4. Deletes the temporary `.pot` file upon completion. +- **Example**: + ```bash + ./scripts/generate_translations.sh ru + ``` + Creates or updates `translations/ru.po`. +- **Requirements**: GNU Gettext tools (`xgettext`, `msgmerge`, `msginit`). +- **Output**: Fresh or updated `.po` files in `translations`, ready for translation and compilation into `.mo` files. + +##### `compile_mo.sh` + +- **Purpose**: Compiles `.po` files from the `translations` directory into `.mo` files and generates a `run.sh` script for launching the program with a chosen language. +- **Usage**: + ```bash + ./scripts/compile_mo.sh + ``` + Replace with the path to your program (e.g., `./build/usr/bin/myapp`). +- **How It Works**: + 1. Compiles each `.po` file into an `.mo` file, placing them in `build/usr/share/locale//LC_MESSAGES/.mo`. + 2. Creates an executable `run.sh` script that: + - Accepts a language code (e.g., `ru` or `en`) as an argument. + - Validates the language against available `.po` files. + - Sets `LANG` and `LD_PRELOAD` environment variables to run the program with the selected localization. +- **Example Launch**: + ```bash + ./run.sh ru + ``` + Runs the program with Russian localization, preloading `.preload/liboverride_bindtextdomain.so`. +- **Output**: Compiled `.mo` files and a `run.sh` script for easy, localized program execution. + +### Applying These Mechanisms in Your Project + +The `preload` mechanism allows overriding localization paths without altering the program’s source code. + +#### Integration Example with `dub.json` + +For seamless automation, integrate these mechanisms into your build process using the `"postBuildCommands"` field in `dub.json`, the configuration file for the DUB build tool. + +Add the following to your `dub.json`: + +```json +{ + "postBuildCommands": [ + "bash scripts/compile_mo.sh ", + "bash preload/build.sh " + ] +} +``` + +Replace with the path to your program (e.g., `./build/usr/bin/myapp`). +Replace `` with your program’s name (e.g., `myapp`). + +##### How It Works: +1. **Build the Project**: Run `dub build` in your project’s root directory. +2. **Post-Build Steps**: + - `bash scripts/compile_mo.sh ./build/usr/bin/myapp`: + - Compiles `.po` files into `.mo` files, storing them in `build/usr/share/locale//LC_MESSAGES/myapp.mo`. + - Generates a `run.sh` script for launching `myapp` with a specified language. + - `bash preload/build.sh myapp`: + - Compiles `.preload/liboverride_bindtextdomain.so` with `PNAME` set to `myapp`. +3. **Run the Program**: + ```bash + ./run.sh ru + ``` + Launches `myapp` with Russian localization, leveraging the preloaded library. diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..0be1a8a --- /dev/null +++ b/dub.json @@ -0,0 +1,40 @@ +{ + "authors": [ + "Alexander Zhirov" + ], + "copyright": "Copyright © 2025, Alexander Zhirov", + "description": "Integrates GNU Gettext for translations", + "license": "GPL-2.0", + "name": "singlang", + "targetType": "dynamicLibrary", + "targetName": "singlang", + "targetPath": "build/usr/lib", + "sourceFiles": ["source/singlang.d"], + "configurations": [ + { + "name": "shared", + "targetType": "dynamicLibrary", + "targetName": "singlang", + "targetPath": "build/usr/lib", + "sourceFiles": ["source/singlang.d"] + }, + { + "name": "static", + "targetType": "library", + "targetName": "singlang", + "targetPath": "build/usr/lib", + "sourceFiles": ["source/singlang.d"] + }, + { + "name": "test", + "targetType": "executable", + "targetName": "test", + "targetPath": "build/usr/bin", + "sourcePaths": ["tests", "source"], + "postBuildCommands": [ + "bash scripts/compile_mo.sh ./build/usr/bin/test", + "bash preload/build.sh test" + ] + } + ] +} diff --git a/dub.selections.json b/dub.selections.json new file mode 100644 index 0000000..322586b --- /dev/null +++ b/dub.selections.json @@ -0,0 +1,5 @@ +{ + "fileVersion": 1, + "versions": { + } +} diff --git a/preload/build.sh b/preload/build.sh new file mode 100755 index 0000000..d0007f2 --- /dev/null +++ b/preload/build.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +if [ -z "$1" ]; then + echo "Error: please specify a value for PNAME" + exit 1 +fi + +mkdir -p .preload + +gcc -DPNAME="\"${1}\"" -shared -fPIC -o .preload/liboverride_bindtextdomain.so ./preload/override_bindtextdomain.c -ldl + +if [ $? -eq 0 ]; then + echo "Library successfully compiled: liboverride_bindtextdomain.so" +else + echo "Compilation error" + exit 1 +fi diff --git a/preload/override_bindtextdomain.c b/preload/override_bindtextdomain.c new file mode 100644 index 0000000..d895647 --- /dev/null +++ b/preload/override_bindtextdomain.c @@ -0,0 +1,26 @@ +#define _GNU_SOURCE +#include +#include +#include + +typedef char* (*bindtextdomain_t)(const char* domainname, const char* dirname); + +char* bindtextdomain(const char* domainname, const char* dirname) { + static bindtextdomain_t original_bindtextdomain = NULL; + if (!original_bindtextdomain) { + original_bindtextdomain = (bindtextdomain_t)dlsym(RTLD_NEXT, "bindtextdomain"); + if (!original_bindtextdomain) { + fprintf(stderr, "Error: failed to find bindtextdomain\n"); + return NULL; + } + } +#ifndef PNAME + fprintf(stderr, "Error: PNAME is not defined. Please define PNAME during compilation.\n"); + exit(1); +#else + if (strcmp(domainname, PNAME) == 0) { + return original_bindtextdomain(domainname, "build/usr/share/locale"); + } + return original_bindtextdomain(domainname, dirname); +#endif +} diff --git a/scripts/compile_mo.sh b/scripts/compile_mo.sh new file mode 100755 index 0000000..aaf4574 --- /dev/null +++ b/scripts/compile_mo.sh @@ -0,0 +1,87 @@ +#!/bin/bash + +TRANSLATIONS_DIR="translations" +SYSTEM_LOCALE_DIR="build/usr/share/locale" + +# Проверка на наличие аргумента +if [ $# -eq 0 ]; then + echo "Error: Please specify program name as argument" + exit 1 +fi + +PROGRAM_PATH="$1" +PROGRAM_NAME=`basename $PROGRAM_PATH` + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +compile_po_to_mo() { + local lang="$1" + local po_file="$TRANSLATIONS_DIR/${lang}.po" + local mo_dir="$SYSTEM_LOCALE_DIR/${lang}/LC_MESSAGES" + local mo_file="$mo_dir/$PROGRAM_NAME.mo" + + if [ -f "$po_file" ]; then + echo -e "${GREEN}Compiling $mo_file...${NC}" + mkdir -p "$mo_dir" + msgfmt -o "$mo_file" "$po_file" + else + echo -e "${YELLOW}Warning: $po_file not found, skipping...${NC}" + fi +} + +get_available_languages() { + ls -1 "$TRANSLATIONS_DIR"/*.po 2>/dev/null | sed "s|$TRANSLATIONS_DIR/||" | sed 's|\.po||' || echo "" +} + +generate_run_script() { + echo -e "${GREEN}Generating run.sh...${NC}" + cat > run.sh << EOF +#!/bin/bash + +if [ \$# -eq 0 ]; then + echo "Error: Please specify a language" + exit 1 +fi + +LANGUAGE=\$1 +LANG_DIR="translations" + +if [ ! -f "\$LANG_DIR/\$LANGUAGE.po" ]; then + echo "Error: Language '\$LANGUAGE' not supported. Available languages:" + ls -1 \$LANG_DIR/*.po | sed 's|.*/||;s|\.po||' + exit 1 +fi + +LANG="\${LANGUAGE}_\${LANGUAGE@U}.UTF-8" +LD_PRELOAD=.preload/liboverride_bindtextdomain.so LANG="\$LANG" $PROGRAM_PATH +EOF + + chmod +x run.sh + echo -e "${GREEN}run.sh generated successfully${NC}" +} + +main() { + if [ ! -d "$TRANSLATIONS_DIR" ]; then + echo -e "${RED}Error: $TRANSLATIONS_DIR directory not found${NC}" + exit 1 + fi + + echo -e "${GREEN}Starting MO files compilation...${NC}" + local languages=$(get_available_languages) + + if [ -z "$languages" ]; then + echo -e "${YELLOW}Warning: No .po files found in $TRANSLATIONS_DIR${NC}" + else + for lang in $languages; do + compile_po_to_mo "$lang" + done + echo -e "${GREEN}MO files compiled successfully!${NC}" + fi + + generate_run_script +} + +main diff --git a/scripts/generate_translations.sh b/scripts/generate_translations.sh new file mode 100755 index 0000000..497b214 --- /dev/null +++ b/scripts/generate_translations.sh @@ -0,0 +1,140 @@ +#!/bin/bash + +TRANSLATIONS_DIR="translations" +POT_FILE="$TRANSLATIONS_DIR/messages.pot" + +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +show_help() { + # Определяем язык системы (берем первые два символа из LANG или LC_ALL) + SYS_LANG="${LC_ALL:-$LANG}" + SYS_LANG="${SYS_LANG:0:2}" + + # По умолчанию английский, если язык не ru + if [ "$SYS_LANG" = "ru" ]; then + echo "Справка по скрипту" + echo "-----------------" + echo "Назначение:" + echo " Этот скрипт автоматизирует создание и обновление файлов переводов (.po)" + echo " для исходных файлов с расширением .d в директории source/." + echo "" + echo "Использование:" + echo " ./$(basename "$0") [язык]" + echo " ./$(basename "$0") [-h | --help]" + echo "" + echo "Параметры:" + echo " [язык] - Необязательный код языка (например, ru, en). Если не указан," + echo " обрабатываются языки ru и en." + echo " -h, --help - Показать эту справку и выйти." + echo "" + echo "Описание работы:" + echo " 1. Проверяет наличие .d-файлов в source/." + echo " 2. Создаёт или обновляет POT-файл (translations/messages.pot) из .d-файлов." + echo " 3. Создаёт или обновляет .po-файлы для указанного языка (или ru и en по умолчанию)." + echo " 4. Удаляет временный POT-файл после завершения." + echo "" + echo "Примеры:" + echo " ./$(basename "$0") # Создать/обновить переводы для ru и en" + echo " ./$(basename "$0") ru # Создать/обновить перевод только для ru" + echo " ./$(basename "$0") -h # Показать эту справку" + echo "" + echo "Требования:" + echo " Установленные утилиты: xgettext, msgmerge, msginit (GNU Gettext)." + else + echo "Script Help" + echo "-----------" + echo "Purpose:" + echo " This script automates the creation and updating of translation files (.po)" + echo " for source files with the .d extension in the source/ directory." + echo "" + echo "Usage:" + echo " ./$(basename "$0") [language]" + echo " ./$(basename "$0") [-h | --help]" + echo "" + echo "Parameters:" + echo " [language] - Optional language code (e.g., ru, en). If not specified," + echo " processes languages ru and en." + echo " -h, --help - Show this help and exit." + echo "" + echo "How it works:" + echo " 1. Checks for .d files in source/." + echo " 2. Creates or updates the POT file (translations/messages.pot) from .d files." + echo " 3. Creates or updates .po files for the specified language (or ru and en by default)." + echo " 4. Removes the temporary POT file after completion." + echo "" + echo "Examples:" + echo " ./$(basename "$0") # Create/update translations for ru and en" + echo " ./$(basename "$0") ru # Create/update translation only for ru" + echo " ./$(basename "$0") -h # Show this help" + echo "" + echo "Requirements:" + echo " Installed utilities: xgettext, msgmerge, msginit (GNU Gettext)." + fi + exit 0 +} + +check_source_files() { + if ! find source/ -name "*.d" | grep -q .; then + echo -e "${RED}Error: No .d files found in source/ or its subdirectories${NC}" + exit 1 + fi +} + +generate_pot_file() { + echo -e "${GREEN}Generating POT file from all .d files in source/ and subdirectories...${NC}" + + if [ -f "$POT_FILE" ]; then + local temp_pot="$TRANSLATIONS_DIR/temp.pot" + find source/ -name "*.d" -exec xgettext --keyword=_ --language=C --from-code=UTF-8 -o "$temp_pot" {} + + msgmerge -U "$POT_FILE" "$temp_pot" + rm "$temp_pot" + else + find source/ -name "*.d" -exec xgettext --keyword=_ --language=C --from-code=UTF-8 -o "$POT_FILE" {} + + fi + + if [ ! -f "$POT_FILE" ]; then + echo -e "${RED}Error: Failed to generate $POT_FILE${NC}" + exit 1 + fi +} + +process_language() { + local lang="$1" + local po_file="$TRANSLATIONS_DIR/${lang}.po" + + if [ ! -f "$po_file" ]; then + echo -e "${GREEN}Creating $po_file...${NC}" + msginit -l "$lang" -i "$POT_FILE" -o "$po_file" --no-translator + sed -i 's/charset=ASCII/charset=UTF-8/' "$po_file" + else + echo -e "${YELLOW}Updating $po_file with existing translations preserved...${NC}" + msgmerge -U "$po_file" "$POT_FILE" + fi +} + +main() { + if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then + show_help + fi + + mkdir -p "$TRANSLATIONS_DIR" + check_source_files + generate_pot_file + + if [ $# -eq 1 ]; then + process_language "$1" + echo -e "${GREEN}PO file for language '$1' generated successfully!${NC}" + else + for lang in ru en; do + process_language "$lang" + done + echo -e "${GREEN}PO files generated successfully!${NC}" + fi + + rm "$POT_FILE" +} + +main "$@" diff --git a/source/singlang.d b/source/singlang.d new file mode 100644 index 0000000..082ee93 --- /dev/null +++ b/source/singlang.d @@ -0,0 +1,147 @@ +/++ + Module singlang - Lightweight internationalization (i18n) support for D programs. + + Description: + This module integrates GNU Gettext for translations in D applications, using a singleton + `Singlang` class and a convenient `_()` function for message translation. Errors are + reported to stderr for proper error handling. ++/ +module singlang; + +import core.stdc.locale : setlocale, LC_ALL; +import std.string : toStringz; +import std.conv : to; +import std.stdio : writeln, stderr; + +extern(C) { + char* bindtextdomain(const char* domainname, const char* dirname); + char* textdomain(const char* domainname); + char* gettext(const char* msgid); +} + +/++ + Class Singlang - Singleton manager for translation configuration. + + Description: + The `Singlang` class encapsulates the setup and management of the GNU Gettext-based + translation system. It ensures that the translation environment is initialized only once + and provides a centralized configuration point for the domain and translation directory. + + Usage Example: + --- + import singlang; + + void main() { + if (Singlang.set("myapp", "/usr/share/locale")) { + writeln("Translation system ready!"); + } + } + --- ++/ +class Singlang { +private: + /++ Translation domain name used by GNU Gettext +/ + string domain; + + /++ Directory path containing translation files (.mo) +/ + string localeDir; + + /++ Singleton instance of the Singlang class +/ + static Singlang instance; + + /++ + Private constructor for initializing a Singlang instance. + + Params: + domain = The translation domain name + localeDir = Path to the directory with translation files + + Note: + Accessible only internally; use `set` to create an instance. + +/ + this(string domain, string localeDir) { + this.domain = domain; + this.localeDir = localeDir; + } + +public: + /++ + Initializes the translation system with a domain and directory. + + Description: + Configures the GNU Gettext environment by setting the translation directory and domain. + This method must be called before using the `_()` function for translations. Errors + are reported to stderr. + + Params: + domain = Name of the translation domain (e.g., application name) + localeDir = Path to the directory with translation files (.mo) + + Returns: + `true` on successful initialization, `false` if an error occurs + + Notes: + - Attempting to initialize an already initialized instance will fail with an error message to stderr. + - Errors during directory or domain setup will reset the instance to null and log messages to stderr. + + Example: + --- + if (Singlang.set("myapp", "/path/to/translations")) { + writeln("Ready to translate!"); + } else { + writeln("Setup failed, check stderr for details!"); + } + --- + +/ + static bool set(string domain, string localeDir) { + if (instance !is null) { + stderr.writeln("Error: Singlang is already initialized"); + return false; + } + + setlocale(LC_ALL, ""); + + instance = new Singlang(domain, localeDir); + + if (bindtextdomain(domain.toStringz, localeDir.toStringz) is null) { + stderr.writeln("Error: Failed to set translations directory"); + instance = null; + return false; + } + + if (textdomain(domain.toStringz) is null) { + stderr.writeln("Error: Failed to set translations domain"); + instance = null; + return false; + } + + return true; + } +} + +/++ + Retrieves the translated version of a given message. + + Description: + Translates a message using the configured `Singlang` instance. If the system is not + initialized, it returns the original message and logs an error to stderr. + + Params: + msg = The message to translate (UTF-8 encoded) + + Returns: + The translated string, or the original message if translation fails or system is uninitialized + + Example: + --- + writeln(_("Error: File not found")); // Outputs translated text or original if unavailable + --- ++/ +string _(string msg) { + if (Singlang.instance is null) { + stderr.writeln("Error: Singlang module is not initialized"); + return msg; + } + auto result = gettext(msg.toStringz); + return result !is null ? result.to!string : msg; +} diff --git a/tests/main.d b/tests/main.d new file mode 100644 index 0000000..b1dc5cc --- /dev/null +++ b/tests/main.d @@ -0,0 +1,27 @@ +module main; + +import singlang; +import shopping; +import std.stdio : writeln; + +void main() { + if (!Singlang.set("test", "/usr/share/locale")) { + writeln("Failed to initialize translations"); + return; + } + + addItem(_("Milk")); + addItem(_("Bread")); + addItem(_("Eggs")); + + printList(); + printSummary(); + + removeItem(1); + + writeln(_("After removing an item:")); + printList(); + printSummary(); + + writeln(_("Happy shopping!")); +} diff --git a/tests/shopping.d b/tests/shopping.d new file mode 100644 index 0000000..a093205 --- /dev/null +++ b/tests/shopping.d @@ -0,0 +1,37 @@ +module shopping; + +import singlang : _; +import std.stdio : writeln; +import std.array : empty; +import std.conv : to; + +string[] items; + +void addItem(string item) { + items ~= item; +} + +void removeItem(size_t index) { + if (index < items.length) { + items = items[0 .. index] ~ items[index + 1 .. $]; + } +} + +void printList() { + if (items.empty) { + writeln(_("Your shopping list is empty")); + return; + } + writeln(_("Shopping List:")); + foreach (i, item; items) { + writeln((i + 1).to!string ~ ". " ~ item); + } +} + +size_t itemCount() { + return items.length; +} + +void printSummary() { + writeln(_("Total items: ") ~ itemCount().to!string); +} diff --git a/translations/en.po b/translations/en.po new file mode 100644 index 0000000..75914d6 --- /dev/null +++ b/translations/en.po @@ -0,0 +1,50 @@ +# English translations for PACKAGE package. +# Copyright (C) 2025 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Automatically generated, 2025. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-03-24 01:51+0300\n" +"PO-Revision-Date: 2025-03-24 01:41+0300\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: en\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: tests/shopping.d:22 +msgid "Your shopping list is empty" +msgstr "Your shopping list is empty" + +#: tests/shopping.d:25 +msgid "Shopping List:" +msgstr "Shopping List:" + +#: tests/shopping.d:36 +msgid "Total items: " +msgstr "Total items: " + +#: tests/main.d:13 +msgid "Milk" +msgstr "Milk" + +#: tests/main.d:14 +msgid "Bread" +msgstr "Bread" + +#: tests/main.d:15 +msgid "Eggs" +msgstr "Eggs" + +#: tests/main.d:22 +msgid "After removing an item:" +msgstr "After removing an item:" + +#: tests/main.d:26 +msgid "Happy shopping!" +msgstr "Happy shopping!" diff --git a/translations/ru.po b/translations/ru.po new file mode 100644 index 0000000..04e8b3b --- /dev/null +++ b/translations/ru.po @@ -0,0 +1,51 @@ +# Russian translations for PACKAGE package. +# Copyright (C) 2025 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Automatically generated, 2025. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-03-24 01:51+0300\n" +"PO-Revision-Date: 2025-03-24 01:41+0300\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: ru\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && " +"n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" + +#: tests/shopping.d:22 +msgid "Your shopping list is empty" +msgstr "Ваш список покупок пуст" + +#: tests/shopping.d:25 +msgid "Shopping List:" +msgstr "Список покупок:" + +#: tests/shopping.d:36 +msgid "Total items: " +msgstr "Всего позиций: " + +#: tests/main.d:13 +msgid "Milk" +msgstr "Молоко" + +#: tests/main.d:14 +msgid "Bread" +msgstr "Хлеб" + +#: tests/main.d:15 +msgid "Eggs" +msgstr "Яйца" + +#: tests/main.d:22 +msgid "After removing an item:" +msgstr "После удаления позиции:" + +#: tests/main.d:26 +msgid "Happy shopping!" +msgstr "Удачных покупок!"