如何使用gatsby使用I18N使用文本格式(粗体,斜体等)?

发布于 2025-01-26 14:21:51 字数 1042 浏览 4 评论 0原文

我正在使用gatsby-plugin-react-i18next来翻译我的网站,并且可以使用简单的文本。但是,当我尝试使用大胆文本或斜体文本格式化文本的部分时,它不起作用(我不知道该怎么做)。

如何使用I18N格式化段落的特定部分?

以下是我设置的一个示例。

Page JS

const IndexPage = () => {

  const { t } = useTranslation();

  return (
    <Layout>
      <Seo title="Home" />
      <div>
          <h1>
            {t("HomepageHeader")} 
          </h1>
          <p>
            {t("HomepageDescription")} 
          </p>
      </div>
    </Layout>
  )
}

export default IndexPage

文件夹结构:

locales

-en

-translation.json

-nl -nl

-translation.json

示例json en

{
"HomepageHeader": "Grow your business with a modern website!",
"HomepageDescription": "Your website is the number 1 selling point, to your customers. Make sure you get the most out of it!"
}

我如何做 HomePagedEscription Bold仅“ 1号卖点”?

I'm using gatsby-plugin-react-i18next for translating my website, and it works with simple text. But when I try to format parts of the text, with bold text or italic text, it doesn't work (I don't know how to do it).

How can I format a specific part of a paragraph using i18n?

Below is an example of my setup.

page JS

const IndexPage = () => {

  const { t } = useTranslation();

  return (
    <Layout>
      <Seo title="Home" />
      <div>
          <h1>
            {t("HomepageHeader")} 
          </h1>
          <p>
            {t("HomepageDescription")} 
          </p>
      </div>
    </Layout>
  )
}

export default IndexPage

Folder structure:

locales

-en

--translation.json

-nl

--translation.json

Example JSON en

{
"HomepageHeader": "Grow your business with a modern website!",
"HomepageDescription": "Your website is the number 1 selling point, to your customers. Make sure you get the most out of it!"
}

How can I make for example only "number 1 selling point" in the HomepageDescription bold?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2025-02-02 14:21:51

看看trans组件: https://react.i18next.com/latest com.com/latest/latest/latest/trans-trans-trans-trans-trans-trans-trans-trans-in,组件

或使用Markdown组件,类似:

import React from 'react';
import Markdown from './Markdown';
import { useTranslation } from '../utils';

export function Text({
  i18nKey,
  ns = 'client-locize-app-loaders',
  i18nOptions = {},
  message,
  defaultValue,
  linkTarget = '_blank',
}) {
  const { t, ready } = useTranslation(ns, { useSuspense: false });
  if (!ready)
    return (
      <Markdown source={message || 'loading...'} noContainer options={{ linkTarget: '_blank' }} />
    );

  return (
    <Markdown
      source={message || t(i18nKey, { defaultValue, ...i18nOptions })}
      noContainer
      options={{ linkTarget }}
    />
  );
}

import React from 'react';
import { Remarkable } from 'remarkable';

export class Markdown extends React.Component {
  constructor(props) {
    super(props);

    this.content = this.content.bind(this);
    this.renderMarkdown = this.renderMarkdown.bind(this);
  }

  componentWillUpdate(nextProps, nextState) {
    if (nextProps.options !== this.props.options) {
      this.md = new Remarkable(nextProps.options);
    }
  }

  content() {
    if (this.props.source)
      return <span dangerouslySetInnerHTML={{ __html: this.renderMarkdown(this.props.source) }} />;

    return React.Children.map(this.props.children, (child) => {
      if (typeof child === 'string') {
        return <span dangerouslySetInnerHTML={{ __html: this.renderMarkdown(child) }} />;
      }
      return child;
    });
  }

  renderMarkdown(source) {
    if (!this.md) this.md = new Remarkable(this.props.options);
    if (this.props.renderInline) return this.md.renderInline(source);

    return this.md.render(source);
  }

  render() {
    const Container = this.props.container;

    if (this.props.noContainer) return this.content();

    return (
      <Container className={this.props.className} style={this.props.style}>
        {this.content()}
      </Container>
    );
  }
}

Markdown.defaultProps = {
  noContainer: false,
  renderInline: false,
  container: 'div',
  options: {},
  className: '',
};

Have a look at the Trans Component: https://react.i18next.com/latest/trans-component

Or use a markdown component, something like:

import React from 'react';
import Markdown from './Markdown';
import { useTranslation } from '../utils';

export function Text({
  i18nKey,
  ns = 'client-locize-app-loaders',
  i18nOptions = {},
  message,
  defaultValue,
  linkTarget = '_blank',
}) {
  const { t, ready } = useTranslation(ns, { useSuspense: false });
  if (!ready)
    return (
      <Markdown source={message || 'loading...'} noContainer options={{ linkTarget: '_blank' }} />
    );

  return (
    <Markdown
      source={message || t(i18nKey, { defaultValue, ...i18nOptions })}
      noContainer
      options={{ linkTarget }}
    />
  );
}

import React from 'react';
import { Remarkable } from 'remarkable';

export class Markdown extends React.Component {
  constructor(props) {
    super(props);

    this.content = this.content.bind(this);
    this.renderMarkdown = this.renderMarkdown.bind(this);
  }

  componentWillUpdate(nextProps, nextState) {
    if (nextProps.options !== this.props.options) {
      this.md = new Remarkable(nextProps.options);
    }
  }

  content() {
    if (this.props.source)
      return <span dangerouslySetInnerHTML={{ __html: this.renderMarkdown(this.props.source) }} />;

    return React.Children.map(this.props.children, (child) => {
      if (typeof child === 'string') {
        return <span dangerouslySetInnerHTML={{ __html: this.renderMarkdown(child) }} />;
      }
      return child;
    });
  }

  renderMarkdown(source) {
    if (!this.md) this.md = new Remarkable(this.props.options);
    if (this.props.renderInline) return this.md.renderInline(source);

    return this.md.render(source);
  }

  render() {
    const Container = this.props.container;

    if (this.props.noContainer) return this.content();

    return (
      <Container className={this.props.className} style={this.props.style}>
        {this.content()}
      </Container>
    );
  }
}

Markdown.defaultProps = {
  noContainer: false,
  renderInline: false,
  container: 'div',
  options: {},
  className: '',
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文