jsverse/transloco-keys-manager

Feature: marker fn supports scope

Alegiter opened this issue · 11 comments

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe

There are situations when I can't use template translation. Also I need to use translation from one scope in other place. Translation API is good than used in one place. But I need little more flex.

Describe the solution you'd like

marker(key, scope?)

or maybe
marker("scope.key") like in comment marker /** t(scope.key) */ when used in scoped component/module

Describe alternatives you've considered

No response

Describe alternatives you've considered

-

Describe alternatives you've considered

Additional context

No response

I would like to make a pull request for this feature

No

@Alegiter I'm not sure I fully understand what you mean here. can you please share an example?

I was looking for this too... i am pretty sure he means he would like a way to use the marker function to scope the translation key and extract it into the scope-file (like marker( test1, admin ) -> creates the key in the admin-scope file)... right now when I use the marker function I can only put the keys in the top-level translation file, right? (at least I haven't found a way to do it otherwise)

@FloNeu, yes, it's exactly what I meant :)

Any workaround to achieve this?

Any workaround to achieve this?

Not an automatic solution as far as I have seen - I thought of generating the key-files for code into a master.json instead of the language-file directly and mark my translations with a 'prefix.'

Like when the template keys use a scope and then separate them manually. Of course, this loses some advantages of the automatic extraction - but you can later run the find-method against the master and identify missing/extra keys that way again.

But I thought it couldn't be too difficult to add this - as the extractor already looks for scope properties when taking keys from classes/templates. But I had no time to look into it yet.

What would be the best way to implement it for keys from code - as we said before an optional 2nd string property on the marker-method - or just prefixing it?

What does @shaharkazaz think on the matter - seems there is some interest in adding this feature.

The only thing missing are the typings. marker('username', {}, 'scope') already works!

@FloNeu @Alegiter You are more then welcome to open a PR for this :)

This solution works for me without any issues.

Add this to the paths of your tsconfig:
"@ngneat/transloco-keys-manager/marker": ["tools/i18n/marker.ts"]

The contents of marker.ts:

/**
 * Custom marker for transloco-keys-manager that supports 3 parameters.
 * https://github.com/ngneat/transloco-keys-manager/issues/132
 * @param key the translation key
 * @param params this parameter is ignored, always pass void 0
 * @param lang the scope and or language
 * @returns
 */
export function marker(key: string, params?: undefined, lang?: string): string {
  return key;
}

Then use the marker like this:

import { marker } from '@ngneat/transloco-keys-manager/marker';
// ...
marker('username', void 0, 'signup');

@christophbuehler Thanks for sharing!
Anyways the signature needs to be fixed, but I think that API wise it's preferred to just support marker('scope.a') to be consistent with the comment extraction API.
What do you think?

A77AY commented

Is it possible to enable this typing? I would like to be able to pass the language / scope

I think it will be difficult to add behavior here as in the comments in the template so that the scope is taken from the directive