Skip to content

Instantly share code, notes, and snippets.

@iam4x
Created August 25, 2015 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iam4x/f3c2b6fca0e8d68377d5 to your computer and use it in GitHub Desktop.
Save iam4x/f3c2b6fca0e8d68377d5 to your computer and use it in GitHub Desktop.
Catch missing translations from `react-intl`
import { IntlMixin } from 'react-intl';
// prevent app to break when translation is missing
// add message a la i18n Rails
export function getIntlMessage(key) {
try {
return IntlMixin.getIntlMessage.call(this, key);
} catch (error) {
const { locale } = this.props;
return `translation missing ${locale}: ${key}`;
}
}
@iam4x
Copy link
Author

iam4x commented Aug 25, 2015

Usage in a component with babel ES7:

import React, { Component } from 'react';
import { getIntlMessage } from 'react-intl-wrapper.js';

class Foo extends Component {

  i18n = getIntlMessage

  render() {
    return <h1>{ this.i18n('foo.bar') }</h1> // returns `translation missing ({ locale }): foo.bar`
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment