next-translate icon indicating copy to clipboard operation
next-translate copied to clipboard

How to use translation with serversideprops

Open mahib480 opened this issue 2 years ago • 3 comments

I am using getServerSideProps in class component. When I am using next-translate/withTranslation api like this

import withTranslation form next-translate/withTranslation
export const getServerSideProps = async (ctx) => {
   console.log("hello");
  return ;
  };
 class Test from React.Component{
  
}
export default withTranslation (Test)

then I am getting error Error: You can not use getInitialProps with getServerSideProps. Please remove getInitialProps. . How can we use next-translate api with getServerSideProps in class componet?

mahib480 avatar Apr 01 '22 13:04 mahib480

How is your _app.js file?

aralroca avatar Apr 01 '22 13:04 aralroca

import { csrfToken } from "../utils/common/csrftoken";

const MyApp = ({ Component, pageProps }) => (
    <>
    <Component {...pageProps} csrfToken={csrfToken}/>
    </>
  );

  export default wrapper.withRedux(MyApp);

mahib480 avatar Apr 03 '22 06:04 mahib480

Using a HOC (wrapper.withRedux(MyApp)) in _app.js is using getInitialProps by default in _app.js.

To avoid it, you can use this:

https://github.com/vinissimus/next-translate/issues/801#issuecomment-1059800473

aralroca avatar Apr 04 '22 18:04 aralroca

I believe this issue has been resolved with Next-translate 2.0, as we have rewritten the plugin and replaced the use of regex with a parser. This update has automatically addressed many of the issues related to attempting to parse items with regex.

I trust that this solution has fully resolved the matter, and as such, I will proceed to close the issue. Additionally, I would like to inform you that we have moved the plugin to this repository: https://github.com/aralroca/next-translate-plugin.

If, for any reason, you find that the issue has not been fully resolved, please feel free to reopen the issue on the next-translate-plugin repository.

Thank you very much.

aralroca avatar Feb 20 '23 17:02 aralroca