Migration guide v15 → v16
Key Highlights
- Support for Angular Standalone Components:
v16 adds full support for Angular’s standalone components, simplifying the integration ofngx-translate
in modern Angular applications. - Bug Fixes and Performance Enhancements:
Several bugs have been fixed, improving overall stability and performance across various scenarios. These fixes ensure smoother operation in multilingual projects. - Code Quality Improvements:
v16 introduces specific types for interfaces, reducing reliance onany
and improving type safety. Additionally, numerous ESLint warnings have been resolved, resulting in cleaner and more maintainable code. - Improved Documentation:
This release introduces a new structured documentation system, moving away from the basic GitHub README. This provides a clearer, more detailed reference guide for users, making it easier to navigate and find necessary information. - License Included in NPM Package:
The MIT license is now included directly in the npm package, ensuring full transparency and compliance for users. This allows developers to clearly understand the terms of use when installing and using@ngx-translate/core
.
By migrating to @ngx-translate/core
, you can ensure your project stays up-to-date, secure, and compatible with the latest Angular features, while benefiting from these new improvements in v16.
1. Update @ngx-translate/core to the new release
Start by updating to the newest version of @ngx-translate/core:
If you are also using @ngx-translate/http-loader, update that one too. With this release, we are now keeping the versions of both modules in sync, making it less confusing:
2. Update Components and Code
2.1 Standalone Components Support
Action: Recommended when using Standalone Components
If your project uses Angular’s standalone components, ensure you’re utilizing the
latest features of @ngx-translate/core
. Refer to the updated documentation
for detailed instructions on how to integrate translations with standalone components.
Here’s a quick overview what you should change.
Configuring ngx-translate
Update your configuration in the app.config.ts. Instead of using importProvidersFrom()
,
you can now use provideTranslateService()
with the same configuration.
Updating your standalone components
In your component, instead of importing the TranslateModule
,
import the TranslatePipe
or TranslateDirective
as used in your code.
2.2 Update Types
Action: Optional
We’ve introduced types to enhance type checking in ngx-translate
, such as
Translation
and TranslationObject
for several functions. However, due to
limitations in TypeScript’s handling of recursively defined types, we are still
forced to include any
in certain parts of these types. As a result, the impact
on your existing code should be minimal.
You can optionally update your code to use these specific types instead of any
for better type safety:
Switching to Translation
type will improve type checking without drastically
affecting your current implementation.
2.3 Using the ngx-translate _()
Marker Function
Action: Optional
If you’re extracting translations directly from your code using a marker function,
you can now use the built-in _()
function provided by ngx-translate
.
This function helps mark strings for automated extractions, e.g. when using BabelEdit or the ngx-translate-extract plugin.
Here’s an example of how to use it:
2.4 TranslateParser: getValue()
Action: Low Impact .
This is only relevant if
you implemented a custom parser and/or use the getValue()
function.
We’ve removed the need to manually implement the getValue()
function in your
custom TranslateParser
. In fact, the implementation in the parser itself
is not used anymore.
The getValue()
function is now directly available from ngx-translate
as
an import, making it easier to use without additional implementation.
If you were previously using the getValue()
function from the default parser,
update your code to use the new direct import:
2.5 TranslateService: Deprecated getTranslation()
Action: Low Impact
The getTranslation()
function, which returned all translations for the given
language in an internal state, has been deprecated. When using a custom
TranslateCompiler
, this function might not only return text nodes but also
functions used for text interpolation.
We do not see a practical use case for continuing to rely on this function.
To retrieve translated text, use functions like get()
or instant()
to
access the specific translations you need.
4. Review Behaviour Changes
use()
Previously, the use()
function relied on the order in which translation files
were loaded from the server, making it non-deterministic when rapidly switching
languages. This could result in earlier language files overriding later selections.
We’ve updated the use()
function to ensure consistent behavior.
-
First Call: When calling
use()
for the first time, it now immediately sets the current language and begins loading the language file. -
Successive Calls: For multiple successive calls, the function will trigger the loading of each requested language. However, the current language will always switch to the language selected by the most recent call, regardless of the order in which the translation files finish loading.
This change ensures that even when making rapid language changes, the most recent language selection is applied correctly.
Interpolations in nested Translation Objects
Starting with ngx-translate
16, all translation functions now apply interpolations
to nested translation objects, addressing a limitation present in previous versions.
Example:
When using a key like main
that contains translations for subkeys such as
main.title
, and main.text
the returned object will include
the keys title
, text
, and footer
.
In versions up to ngx-translate
15: old
-
Issue: Parameters inside nested translation objects were not interpolated.
-
Result: The placeholders (e.g.,
{name}
) remained as-is, and the returned object looked like this:
In ngx-translate
16: new
-
Improvement: Parameters inside nested translation objects are now fully interpolated.
-
Result: The placeholders are replaced with actual values, producing the following output:
3. Run and Test Your Application
After making the changes, run your application to verify that everything works as expected. Make sure all translations are loading correctly and there are no errors related to the library.
4. Stay Updated
Keep track of updates and improvements by following the library on GitHub: GitHub repository.
We welcome any feedback or issues you encounter during the migration process.