css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <p>
  The following demonstrates manually integrating i18next with React.
  Unfortunately, there is no easy way to get your internationalized component to
  automatically re-render when the language changes without tight coupling and
  boilerplate setup of languageChanged listeners.
</p>
<p>
  Therefore, it is recommended to use a better solution like what is offered by
  the react-i18next package. See <a href="https://codepen.io/alligatorio/pen/PJvBZN?editors=1010" target="codepen_boJZML">this pen</a> for a demo.
</p>
<p id="placeholder"></p>
<button>Toggle Language</button>

            
          
!
            
              label::after {
  content: ':'
}

            
          
!
            
              const i18n = i18next.init({
    interpolation: {
      // React already does escaping
      escapeValue: false,
    },
    lng: 'en', // 'en' | 'es'
    resources: {
      en: {
        translation: {
          age: { label: 'Age', },
          home: { label: 'Home', },
          name: { label: 'Name', },
        },
      },
      es: {
        translation: {
          age: { label: 'Años', },
          home: { label: 'Casa', },
          name: { label: 'Nombre', },
        },
      },
    },
  },
  (err, t) => {
    if (err) {
      return console.error(err)
    }
    console.log('i18n successfully initialized')
  }
)

class StateGator extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      lng: 'en'
    }

    // Boilerplate
    this.onLanguageChanged = this.onLanguageChanged.bind(this)
  }

  // Boilerplate
  componentDidMount() {
    i18n.on('languageChanged', this.onLanguageChanged)
  }

  // Boilerplate
  componentWillUnmount() {
    i18n.off('languageChanged', this.onLanguageChanged)
  }

  // Boilerplate
  onLanguageChanged(lng) {
    this.setState({
      lng: lng
    })
  }

  render() {
    let gator = this.props.gator,
        lng = this.state.lng

    return (
      <div>
        <label>{i18n.t('name.label', { lng })}</label>
        <span>{ gator.name } 🐊</span> <br/>
        <label>{i18n.t('age.label', { lng })}</label>
        <span>{ gator.age }</span> <br/>
        <label>{i18n.t('home.label', { lng })}</label>
        <span>{ gator.home }</span>
      </div>
    )
  }
}

class ForceUpdateGator extends React.Component {

  constructor(props) {
    super(props)

    // Boilerplate
    this.onLanguageChanged = this.onLanguageChanged.bind(this)
  }

  // Boilerplate
  componentDidMount() {
    i18n.on('languageChanged', this.onLanguageChanged)
  }

  // Boilerplate
  componentWillUnmount() {
    i18n.off('languageChanged', this.onLanguageChanged)
  }

  // Boilerplate
  onLanguageChanged(lng) {
    this.forceUpdate()
  }

  render() {
    let gator = this.props.gator

    return (
      <div>
        <label>{i18n.t('name.label')}</label>
        <span>{ gator.name } 🐊</span> <br/>
        <label>{i18n.t('age.label')}</label>
        <span>{ gator.age }</span> <br/>
        <label>{i18n.t('home.label')}</label>
        <span>{ gator.home }</span>
      </div>
    )
  }
}

document.addEventListener('DOMContentLoaded', () => {
  document.querySelector('button').addEventListener('click', () => {
    if (i18n.language !== 'en') {
      i18n.changeLanguage('en')
    }
    else {
      i18n.changeLanguage('es')
    }
  })

  let gator = {
    name: 'Alli',
    age: 8,
    home: 'Bayou',
  }
  ReactDOM.render(
    (
      <div>
        <h3>Using State</h3>
        <StateGator gator={gator}/>
        <h3>Using forceUpdate</h3>
        <ForceUpdateGator gator={gator}/>
      </div>
    ),
    document.querySelector('#placeholder')
  )
})

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console