-
-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat): Namespace support #1284
base: master
Are you sure you want to change the base?
Conversation
preparation to add a namespace-translate directive
preparation to add namespace translate pipe
used in the namepsace translate service, directive and pipe tests
angular doesn't allow the usage of function in providers with the default settings.
@ocombe Would be cool if this gets merged 😉 |
} | ||
} | ||
|
||
// const namespaceTranslateFactory = (namespace: string) => (translate: TranslateService) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgotten commented code
Hello @ReschAndr, @ocombe I came across this PR because I have a very similar implementation in my project. I define translations per component, and use namespaces to avoid collisions between translation keys. I checked the code and tests and currently don't noticed something missing. I confirm that it would be usefull to have namespace support where ever withing this projet, or as separate plugin. |
loader: { provide: TranslateLoader, useClass: FakeLoader } | ||
}) | ||
], | ||
providers: [{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be:
providers: [
{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" },
NamespaceTranslateService
]
loader: { provide: TranslateLoader, useClass: FakeLoader } | ||
}) | ||
], | ||
providers: [{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be:
providers: [
{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" },
NamespaceTranslateService
]
@@ -0,0 +1,273 @@ | |||
import { ChangeDetectionStrategy, Component, ElementRef, Injectable, ViewChild, ViewContainerRef } from '@angular/core'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to call this file namespace-translate.directive.spec.ts
(with dash)
imports: [ | ||
TranslateModule.forRoot() | ||
], | ||
declarations: [App] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing:
providers: [
{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" },
NamespaceTranslateService
],
@@ -0,0 +1,272 @@ | |||
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Injectable, ViewContainerRef } from "@angular/core"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to call this file namespace-translate.pipe.spec.ts
(with dash)
loader: { provide: TranslateLoader, useClass: FakeLoader } | ||
}) | ||
], | ||
providers: [{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" }], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Must be:
providers: [
{ provide: TRANSLATION_NAMESPACE, useValue: "NAMESPACE" },
NamespaceTranslateService
],
Sorry if this sounds like criticism but I find this new feature overly complexe to use. If I understand it correctly, it introduces a new service and new Pipe with an injection token that has to be provided to defined the namespace. Having the namespace provided means it has to be static, What if we want to have a dynamic namespace? I briefly made a test on my side. This is not meant to be complete but I've been able to make something that would work almost seamlessly in existing components with the translate pipe. Usage in template:
TranslateContextComponent
I'm not sure yet if it's a bad idea to extend the TranslateService... Worst case, I would implement all methods of TranslateService, inject the real TranslateService using the @SkipSelf constructor decorator and proxy all functions calls to it. |
I've added my implementation: |
First of all this PR does not contain breaking changes.
The only issue could be, that the @biesbjerg/ngx-translate-extract tool doesn't support extracting the keys from the namespace translate service, pipe, and directive yet, but I will also provide a PR on their repo to add support for that.
Motivation
We are currently working on migrating an existing web application (asp.net forms) to angular. Until now we used our own low-key namespace-translate pipe, which came with the issue of not being able to use the ngx-translate-extract tool. With this pr, I want to add the functionality of not having to define the absolute path of the translation key within very deep nested components. This will prevent spelling mistakes within the key and should help code more orderly.
Added Components
NamespaceTranslateService
The service should be provided on the component level with the
namespaceTranslateServiceProvider
helper.It provides functions to get the translation for a specific key(s) which will be prefixed with the namespace given to the
namespaceTranslateServiceProvider
function.NamespaceTranslatePipe
The NamespaceTranslatePipe provides the same functionality as the TranslatePipe but prefixes all keys with the namespace given to the namespaceTranslateServiceProvider.
NamespaceTranslateDirective
The NamespaceTranslateDirectiveprovides the same functionality as the TranslateDirective but prefixes all keys with the namespace given to the namespaceTranslateServiceProvider.
Comparison
Without Namespace Translate
With Namespace Translate