Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use <Text/> node as translated string on object #35

Closed
acangiani opened this issue Jul 10, 2019 · 2 comments
Closed

Can't use <Text/> node as translated string on object #35

acangiani opened this issue Jul 10, 2019 · 2 comments

Comments

@acangiani
Copy link

acangiani commented Jul 10, 2019

On the project I'm currently working we needed to support several languages so we ended upusing preact-helmet to inject the title and the corresponding metatags for each of the App views, but I'm unable to make it work with {{fields}} placeholders, so I created this sample project to demonstrate the issue.

How to install and run the sample project

  • Clone the repository with git clone git@github.com:acangiani/preact-i18n-issue.git
  • Install the dependencies: npm install
  • Run the project npm run start

First view

This one works fine and correctly adds the title and the title metatag.

Doing curl http://localhost:3000/, this outputs the following html:

...
<title>Foo - Bar</title>
<meta name="title" content="Foo - Bar" data-preact-helmet="true">
...

Second view

On the other hand on this view I needed to use a {{field}} placeholder, so doing curl http://localhost:3000/test, this outputs the following html:

...
<title>test - Bar</title>
<meta name="title" content="[object Object]" data-preact-helmet="true">
...

Things I tried

  1. Use @withText as decorator on the second view, but I cannot access the props.
  2. Tried to use withText as a functional component wrapper so to obtain the translated text but I couldn't make it work.
  3. Tried to render the component to a string like:
render(<Text id="second.title" " fields={{ field: props.slug }}>default text</Text>)

but I only obtained the default text regardless of the i18n definitions loaded on the IntlProvider.

Bottom line what I need it's to obtain the translated text as a string but I'm unable to do so, could you please help with this?

@pl12133
Copy link
Contributor

pl12133 commented Jul 10, 2019

@acangiani Thanks for the report. withText is also capable of accepting a function which will receive props, and resolve <Text> components with fields. I gave your App a try and the following changes seemed to make the title render correctly:

@withText((props) => ({
  title: <Text id="second.title" fields={{ field: props.slug }} />
}))
export default class SecondView extends Component {
  render(props, state) {
    return (
      <div>
        <Helmet
          title={props.title}
          meta={[
            {name: "title", content: props.title },
          ]}
        />
      </div>
    );
  }
};

Let me know if that resolves the issue.

@acangiani
Copy link
Author

@pl12133 your code resolves the issue!

Thank you for your time and for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants