Search code examples
linguijs

LinguiJS: remove obsolete entries while keeping manually added ones


I'm trying to remove obsolete entries from my lingui catalogs by using lingui extract --clean. However, the catalogs contain entries that I added by hand, which get removed by this as well. For code like this i18n._("myI18nEntryKey") I added entries like this:

"myI18nEntryKey": {
    "translation": "my entry value",
    "obsolete": true
}

Is there a way to preserve them or a better workflow for having both auto-detected i18n entries and manually defined ones? Maybe two catalogs compiled into one?


Solution

  • However, the catalogs contain entries that I added by hand, which get removed by this as well. For code like this i18n._("myI18nEntryKey") I added entries like this:

    This is incorrect. Catalogs are artifacts produced from your code. You shouldn't add any entries manually. It's similar as if you will add something to your js files compiled from ts.

    What you probably need is lazy translation:

    Imagine you have an error codes (or strings) which you receive from backend and want to show a translated message on frontend:

    import {msg} from "@lingui/macro";
    
    // Note: `msg` macro was added in Lingui V4
    // prior V4 its `defineMessage({message: `MyMessage`})`
     
    export const errors = {
     "No user": msg`No user`,
     "No permissions": msg`No permissions`
    }
    
    function showError(backendMsg: string) {
      i18n._(errors[backendMsg])
    }
    

    Then all your "manual" messages would be extracted properly.