flat-fuchsia
flat-fuchsia11mo ago

TSX page format DateField with i18n locale

How on my custom TSX page format DateField with i18n locale? I cannot obtain current i18n locale by code
7 Replies
kapa.ai
kapa.ai11mo ago
Hi @atti_effe. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To format a DateField with i18n locale in your custom TSX page, you can use the format prop of the DateField component. The DateField component uses Day.js to display date format. Here is an example of how you can use it:
import {
List,
DateField,
} from "@refinedev/antd";
import { Table } from "antd";

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="50%" />
<Table.Column
dataIndex="createdAt"
title="Created At"
render={(value) => (
<DateField format="LLL" value={value} />
)}
width="50%"
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
createdAt: string;
}
import {
List,
DateField,
} from "@refinedev/antd";
import { Table } from "antd";

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" width="50%" />
<Table.Column
dataIndex="createdAt"
title="Created At"
render={(value) => (
<DateField format="LLL" value={value} />
)}
width="50%"
/>
</Table>
</List>
);
};

interface IPost {
id: number;
title: string;
createdAt: string;
}
In this example, the format prop is set to "LLL", which is a locale-specific format string. You can replace "LLL" with any valid format string according to your needs. Regarding obtaining the current i18n locale, the provided extracts do not contain specific information on how to do this in refine. However, refine supports i18n out of the box and you can add i18n support to your app using the i18nProvider property. For more detailed information, you can refer to the i18n documentation (https://refine.dev/docs/api-reference/core/providers/i18n-provider/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@573269471595200513> kapa.ai is still learning and improving, please let us know how it did by reacting below
flat-fuchsia
flat-fuchsia11mo ago
@kapa.ai how to keep FORMAT from curent i18n locale?
kapa.ai
kapa.ai11mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The provided extracts do not contain specific information on how to get the current locale's format using refine. However, refine provides the useSetLocale hook which returns the changeLocale method from i18nProvider. This method can be used to change the locale at runtime. Here is an example of how you can use it:
import { useSetLocale } from "@refinedev/core";

export const LanguageSwicher = () => {
const changeLanguage = useSetLocale();

return (
<div>
<span>Languages</span>
<button onClick={() => changeLanguage("en")}>English</button>
<button onClick={() => changeLanguage("es")}>Spanish</button>
</div>
);
};
import { useSetLocale } from "@refinedev/core";

export const LanguageSwicher = () => {
const changeLanguage = useSetLocale();

return (
<div>
<span>Languages</span>
<button onClick={() => changeLanguage("en")}>English</button>
<button onClick={() => changeLanguage("es")}>Spanish</button>
</div>
);
};
In this example, the changeLanguage function can be used to change the locale to either English or Spanish. However, this does not directly provide the format of the current locale. For more detailed information on how to use i18n in refine, you can refer to the i18n documentation (https://refine.dev/docs/api-reference/core/providers/i18n-provider/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@573269471595200513> kapa.ai is still learning and improving, please let us know how it did by reacting below
extended-salmon
extended-salmon11mo ago
Hello @atti_effe is your problem solved?
flat-fuchsia
flat-fuchsia11mo ago
@batuhanw yes thank you