Skip to content

Instantly share code, notes, and snippets.

@timofei-iatsenko
Created October 20, 2024 11:44
Show Gist options
  • Save timofei-iatsenko/876706f265d725d0aac01018f1812b39 to your computer and use it in GitHub Desktop.
Save timofei-iatsenko/876706f265d725d0aac01018f1812b39 to your computer and use it in GitHub Desktop.
Migrate Lingui `t(i18n)` to `msg` (Lingui V5)

Migrate Lingui t(i18n) to i18n._(msg) (Lingui V5)

https://docs.grit.io/cli/quickstart

engine marzano(0.1)
language js


private pattern handle_custom_i18n() {
    any {
        js"$t($i18n)`$message`" => js"$i18n._(msg`$message`)" where {
            $t <: "t",
            $t <: imported_from(from=`'@lingui/core/macro'`),
            $msg = `msg`,
            $msg <: ensure_import_from(`"@lingui/core/macro"`),
        },

        js"$t($i18n)($descriptor)" => js"$i18n._(msg($descriptor))" where {
            $t <: "t",
            $t <: imported_from(from=`'@lingui/core/macro'`),
            $msg = `msg`,
            $msg <: ensure_import_from(`"@lingui/core/macro"`),
            
        }
    }
}

sequential { 
    maybe bubble file($body) where $body <: contains handle_custom_i18n(),
    maybe bubble file($body) where $body <: contains remove_unused_imports(),
}

Test case with TaggetTemplateLiteral

import {t} from "@lingui/core/macro";
let i18n;

t(i18n)`Ola!`
import { msg} from "@lingui/core/macro";
let i18n;

i18n._(msg(`Ola!`))

Test case with MessageDescriptor

import {t} from "@lingui/core/macro";
let i18n;
t(i18n)({message: `Ola!`})
import { msg} from "@lingui/core/macro";
let i18n;
i18n._(msg({message: `Ola!`}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment