I18n: Difference between revisions

From neuromatch
(Created page with "Up to: Part Of::Tech WG, Part Of::Mastodon/Docs Rather than hardcoding strings, Mastodon uses an i18n system to replace strings with versions in different languages. == Usage == An example of how this works would be eg. in the [https://github.com/NeuromatchAcademy/mastodon/blob/main/app/javascript/flavours/glitch/features/ui/components/favourite_modal.jsx favourite modal]: <syntaxhighlight lang="javascript"> import { defineMessages, injectIntl, FormattedMes...")
 
No edit summary
 
Line 11: Line 11:


const messages = defineMessages({
const messages = defineMessages({
   favorite: { id: 'status.favorite', defaultMessage: 'Favorite' },
   favorite: {  
    id: 'status.favorite',  
    defaultMessage: 'Favorite'  
  },
});
});


// some time later..
// some time later..
<Button text={intl.formatMessage(messages.favourite)} onClick={this.handleFavourite} ref={this.setRef} />
<Button  
  text={intl.formatMessage(messages.favourite)}  
  onClick={this.handleFavourite}  
  ref={this.setRef}  
/>
</syntaxhighlight>
</syntaxhighlight>


Line 21: Line 28:


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
<FormattedMessage id='favourite_modal.combo' defaultMessage='You can press {combo} to skip this next time' values={{ combo: <span>Shift + <Icon id='star' /></span> }} />
<FormattedMessage  
  id='favourite_modal.combo'  
  defaultMessage='You can press {combo} to skip this next time'  
  values={{ combo: <span>Shift + <Icon id='star' /></span> }}  
/>
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 18:56, 8 October 2023

Up to: Tech WG, Mastodon/Docs

Rather than hardcoding strings, Mastodon uses an i18n system to replace strings with versions in different languages.

Usage

An example of how this works would be eg. in the favourite modal:

import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';

const messages = defineMessages({
  favorite: { 
    id: 'status.favorite', 
    defaultMessage: 'Favorite' 
  },
});

// some time later..
<Button 
  text={intl.formatMessage(messages.favourite)} 
  onClick={this.handleFavourite} 
  ref={this.setRef} 
/>

Or to use a specific FormattedMessage component:

<FormattedMessage 
  id='favourite_modal.combo' 
  defaultMessage='You can press {combo} to skip this next time' 
  values={{ combo: <span>Shift + <Icon id='star' /></span> }} 
/>

Translations

Each of these keys like 'status.favorite' is defined in a set of JSON files in

  • mastodon/app/javascript/flavours/glitch/locales
  • mastodon/app/javascript/mastodon/locales

These translations can be contributed to using Crowdin:

https://crowdin.com/project/mastodon

CI actions

CI uses formatjs to check the localization files.

This extracts all the messages that are defined in-place using defineMessages and exports them to the en.json locale.