Stirling-PDF/HowToAddNewLanguage.md
Ludy b37457b41d
Add: Configurable UI Language Support with Dynamic Filtering (#2846)
# Description of Changes

### Summary
- Added support for configuring UI languages via `settings.yml`
(`languages` field).
- Modified `LanguageService` to respect the configured languages, while
ensuring British English (`en_GB`) is always enabled.
- Updated Thymeleaf templates to dynamically display only the allowed
languages.
- Improved logging and refactored some list-to-set conversions for
better efficiency.

### Why the Change?
- Allows administrators to limit available UI languages instead of
displaying all detected languages.
- Provides better customization options and simplifies language
management.

### Challenges Encountered
- Ensuring backwards compatibility: If `languages` is empty, all
languages remain enabled.
- Handling `Set<String>` instead of `List<String>` in `LanguageService`
for optimized lookups.

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [x] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [x] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [x] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
2025-02-03 10:52:34 +00:00

3.7 KiB


Stirling-PDF

How to add new languages to Stirling-PDF

Fork Stirling-PDF and create a new branch out of main.

Then add a reference to the language in the navbar by adding a new language entry to the dropdown:

Any SVG flags are fine; most of the current ones were sourced from here. If your language isn't represented by a flag, choose a similar one, such as Saudi Arabia's flag for Arabic.

For example, to add Polish, you would add:

<a th:if="${#lists.isEmpty(@languages) or #lists.contains(@languages, 'pl_PL')}" class="dropdown-item lang_dropdown-item" href="" data-bs-language-code="pl_PL"> <img th:src="@{'/images/flags/pl.svg'}" alt="icon" width="20" height="15"> Polski</a>

The data-bs-language-code is the code used to reference the file in the next step.

Add Language Property File

Start by copying the existing English property file:

Copy and rename it to messages_{your data-bs-language-code here}.properties. In the Polish example, you would set the name to messages_pl_PL.properties.

Then simply translate all property entries within that file and make a Pull Request (PR) into main for others to use!

If you do not have a Java IDE, I am happy to verify that the changes work once you raise the PR (but I won't be able to verify the translations themselves).

Handling Untranslatable Strings

Sometimes, certain strings in the properties file may not require translation because they are the same in the target language or are universal (like names of protocols, certain terminologies, etc.). To ensure accurate statistics for language progress, these strings should be added to the ignore_translation.toml file located in the scripts directory. This will exclude them from the translation progress calculations.

For example, if the English string error=Error does not need translation in Polish, add it to the ignore_translation.toml under the Polish section:

[pl_PL]
ignore = [
    "language.direction",  # Existing entries
    "error"                # Add new entries here
]

Add New Translation Tags

Important

If you add any new translation tags, they must first be added to the messages_en_GB.properties file. This ensures consistency across all language files.

  • New translation tags must be added to the messages_en_GB.properties file to maintain a reference for other languages.
  • After adding the new tags to messages_en_GB.properties, add and translate them in the respective language file (e.g., messages_pl_PL.properties).

Make sure to place the entry under the correct language section. This helps maintain the accuracy of translation progress statistics and ensures that the translation tool or scripts do not misinterpret the completion rate.

Use this code to perform a local check

Windows command

python .github/scripts/check_language_properties.py --reference-file src\main\resources\messages_en_GB.properties --branch "" --files src\main\resources\messages_pl_PL.properties

python .github/scripts/check_language_properties.py --reference-file src\main\resources\messages_en_GB.properties --branch "" --check-file src\main\resources\messages_pl_PL.properties