Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>

              
            
!

CSS

              
                label::after {
  content: ':'
}

              
            
!

JS

              
                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

Console