question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@lingui/loader inside GatsbyJS project throws an error

See original GitHub issue

Describe the bug I have a project where I can successfully use <Trans id="hello" />, but i18n.t`hello` doesn’t work in development (just prints the key), and when building the static site it gives the error WebpackError: i18n.t is not a function.

To Reproduce

import { I18n, Trans } from "@lingui/react"

export default function App() {
  return (
    <I18n>
      {({ i18n }) => (
        <React.Fragment>
          <div>
            {i18n.t`hello`} // --> doesn't work, renders hello
          </div>
          <div>
            <Trans id="hello" /> // --> works, renders bonjour
          </div>
        </React.Fragment>
      )}
    </I18n>
  )
}

Expected behavior Should render bonjour, bonjour instead of hello, bonjour in dev, and should not raise build errors.

Additional context Add any other context about the problem here.

  • jsLingui version 2.6.1
  • Babel version babel-core@6.26.3
  • Using Gatsby v1 and with this (partial) package.json config:
{
  "dependencies": {
    "@lingui/react": "2.6.1",
    "babel-plugin-transform-class-properties": "6.24.1",
    "babel-plugin-transform-object-rest-spread": "6.26.0",
  },
  "devDependencies": {
    "@lingui/babel-preset-react": "2.6.1",
    "@lingui/cli": "2.6.1",
    "babel-plugin-dynamic-import-webpack": "1.0.2",
    "babel-plugin-import": "1.8.0",
    "babel-plugin-syntax-dynamic-import": "6.18.0",
    "babel-webpack-plugin": "0.1.1",
  },
  "scripts": {
    "lingui:extract": "BABEL_ENV=lingui-extract lingui extract",
  },
  "babel": {
    "presets": [
      "env",
      "react",
      "@lingui/babel-preset-react"
    ],
    "env": {
      "lingui-extract": {
        "plugins": [
          "transform-class-properties",
          "transform-object-rest-spread",
          "syntax-dynamic-import",
          "dynamic-import-webpack"
        ]
      }
    }
  },
  "lingui": {
    "fallbackLocale": "en",
    "sourceLocale": "en",
    "localeDir": "src/locale",
    "srcPathDirs": [
      "src/components",
      "src/layouts",
      "src/pages"
    ],
    "format": "minimal"
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
tricoder42commented, Sep 7, 2018

Ah, I see, because plural actually performs formatting and you need a compiled catalog for this.

Alright, here’s the plan: I’ll review the workflow over the weekend. In some cases compiled message catalogs aren’t required and it might be enough to compile them at runtime. I’ll create a documentation for GatsbyJS, example in Gatsby repo and review few starters I saw around.

I’ll get back to you on Monday.

In the meantime, I added PR to your repo.

1reaction
tricoder42commented, Sep 9, 2018

Hmm, since GatsbyJS uses webpack, you might want to use @lingui/loader and change the imports:

- en: require("./locale/en/messages.js"),
+ en: require("@lingui/loader!./locale/en/messages.json"),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Common Errors - Gatsby
Problems with the cache · Errors with common plugin configurations. Installing plugins for styling results in Generating SSR bundle failed · Errors in...
Read more >
Gatsby works fine during development but throws error during ...
One way to make sure the build doesn't throw an error is to: Check for the window in componentDidMount; Map the location prop...
Read more >
Gatsby Changelog | 5.3.0
You can read more about the project on dev.to ... In normal circumstances the errors thrown by GraphQL or Gatsby itself will give...
Read more >
How To Use WordPress Content with a Gatsby.js Application
In this tutorial, you will provision WordPress to talk to Gatsby, set up a new Gatsby project based on a starter template, define...
Read more >
GatsbyJS and Contentful in five minutes
Working with React and having an emphasis on speedy performance, GatsbyJS is a promising static site generator that allows you to connect your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found