Skip to content

Conversation

@KubaJastrz
Copy link
Contributor

@KubaJastrz KubaJastrz commented Feb 2, 2025

Support syntax and ph labels introduces in lingui/js-lingui#2092

import {t} from "@lingui/core/macro"
t`Hello ${{name: getUsername()}}`
import {Trans} from "@lingui/react/macro"
<Trans>Hello {{name: getUsername()}}</Trans>
import {t, ph} from "@lingui/core/macro"
t`Hello ${ph({name: getUsername()})}`
import {Trans, ph} from "@lingui/react/macro"
<Trans>Hello {ph({name: getUsername()})}</Trans>

closes #99

@codecov
Copy link

codecov bot commented Feb 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.75%. Comparing base (fec90fe) to head (3f41b1c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #100      +/-   ##
==========================================
+ Coverage   97.70%   97.75%   +0.05%     
==========================================
  Files          10       10              
  Lines         479      490      +11     
  Branches      147      151       +4     
==========================================
+ Hits          468      479      +11     
  Misses         11       11              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@timofei-iatsenko
Copy link
Collaborator

Thanks for your contribution. Planned to do in the future together with bigger announcement of this feature. What i also wanted to implement for the eslint rule are:

  1. More descriptive error messages and auto fix. Instead of just complaining on expression it's better to give an example how to write this placeholder and implement auto-fix functionality.
  2. Testing for the version. So if the user is using lingui core lower than specified version the error message and autofix would not be available.

I shed a bit of light on the future plans. This placeholder implementation is our ticket to make AST macro completely optional. It means all DX and features lingui currently has will be available without transpilation at all. This will come with some syntax limitations, one of them is the inability to infer name of the variable used in expressions.

To make transition to the newer approach smoother, I planned to implement an automatic migration (using esling autofix). So, users will make their codebase compatible with a new approach even before this approach would land into the core.

Let me know, how you would like to proceed. I will understand if that level of commitment is not what you planned.

@KubaJastrz
Copy link
Contributor Author

Fair points. I thought about different messages, but wanted to push out a fix to allow the upgrade to lingui 5.2 in our team (we really don't like the requirement of intermediate variables for translations 😅).

Do you think it's worth pushing this quick fix (even with a tame release notes so people are not excited) or should we wait for a fully fledged functionality? I'm down to contribute this in another PR if you'd like 🙂

@timofei-iatsenko
Copy link
Collaborator

I reviewed your changes carefully one more time, let's at least add another message for this case:

    {
      code: '<Trans>hello {{name: obj.foo, surname: obj.bar}}</Trans>',
      errors: [{ messageId: 'default' }],
    },

Here i would expect an explicit error, something like:

Invalid placeholder: Expected an object with a single key-value pair (e.g., {{name: obj.foo}}), but found multiple keys ({{name, surname}})."

Would be nice if you would read keys from AST, if not, hard-coded value would be enough.

After that we can merge it as-is, and then implement an autofix and proposition to use named variable in the next PR.

@KubaJastrz
Copy link
Contributor Author

I tried doing fancy stuff with the message, but {{ }} characters are reserved for placeholders and I haven't any found any way around that.

Even though eslint allows that: https://github.com/typescript-eslint/typescript-eslint/blob/bcdf56017a1c9d66d701d2104651ae9d9cf035dd/packages/rule-tester/src/utils/interpolate.ts#L30-L31
There's additional check for unsubstituted placeholders: https://github.com/typescript-eslint/typescript-eslint/blob/bcdf56017a1c9d66d701d2104651ae9d9cf035dd/packages/rule-tester/src/RuleTester.ts#L1063-L1073

@timofei-iatsenko
Copy link
Collaborator

@KubaJastrz good job. LGTM.

PS

Noticed on your github profile that you're learning Rust on your spare time, would you like to test your knowledge on something that will bring a real value and port this feature lingui/js-lingui#2092 into https://github.com/lingui/swc-plugin i can guide you thru the process

@timofei-iatsenko timofei-iatsenko self-requested a review February 4, 2025 07:49
@KubaJastrz
Copy link
Contributor Author

@timofei-iatsenko sure, I'll see what I can do

@andrii-bodnar andrii-bodnar merged commit c33e2d5 into lingui:main Feb 4, 2025
4 checks passed
@KubaJastrz KubaJastrz deleted the ph-labels branch February 4, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no-expression-in-message: support ph and explicit labels from v5.2

3 participants