Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

javascript - Pass localization into an svg image?

Currently, I'm working on a multi-language project in React using i18next.

I have a few SVG images that contain a static text language. This is an example from one of the SVG images.

<text
  stroke="none"
  style="outline-style:none;"
  id="text4"
  stroke-width="1px"
  x="301.069519px"
  text-rendering="geometricPrecision"
  font-family="SinhalaSangamMN"
  fill="#000000"
  font-size="32px"
  y="263.101746px"
  transform=""
  text-anchor="middle"
>
  cieplna < ~~~~~~~~~~ text I want to change
</text>;

Is it possible to pass localization into the SVG file using the i18Next utility that I have been using in my React Components:

i18next.js


const checkLocale = (lng) => {
  return getLocale(lng);
}

export function init18n() {
  const i18nOpts = {
    lng: window.userLang,
    fallbackLng: 'it',
    debug: true,
  }
  return checkLocale(window.userLang).then(res => {
      if (res) {
        i18nOpts.backend = {
          loadPath: `${env.REACT_APP_API_URL}locales/{{lng}}/{{ns}}.json`,
          crossDomain: true,
        }
      }
      return init(i18nOpts);
  })
  .catch(err =>{
    return init(i18nOpts);
  })
}

React example: label={i18next.t( 'windows.MAIN_CARD.projectName' )}

If not, is this something I can do from local storage?

Local Storage window.localStorage.i18nextLng

In this Stack Overflow question Angular / svg - pass data into svg & in this blog post https://getlocalization.wordpress.com/2016/11/07/how-to-utilize-svg-in-localization/, it was demonstrated this was possible.

Not sure where to go from here, thanks in advance for any help.

question from:https://stackoverflow.com/questions/65848535/pass-localization-into-an-svg-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes you can do it. In your component, starts by adding this at the beginning :

const { t, i18n } = useTranslation();

Then, instead of writing cieplna, write :

t('path.to.your.cielplna.translation')

The translation has to be stored in a json file.

Look at the react i18 next documentation, or the different examples that you can find, here for example. https://github.com/i18next/react-i18next/tree/master/example/react/src
The translations are stored in the public/locales folder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...