Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding it * added intl polyfill for safari * had forgotten react-intl in package.json * added app-specific localization data * remove intl page and add intl in demo pages * run ava with harmony-proxies enabled in node
- Loading branch information
Showing
26 changed files
with
351 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
locale: 'en', | ||
messages: { | ||
siteTitle: 'MERN Starter Blog', | ||
addPost: 'Add Post', | ||
switchLanguage: 'Switch Language', | ||
twitterMessage: 'We are on Twitter', | ||
by: 'By', | ||
deletePost: 'Delete Post', | ||
createNewPost: 'Create new post', | ||
authorName: 'Author\'s Name', | ||
postTitle: 'Post Title', | ||
postContent: 'Post Content', | ||
submit: 'Submit', | ||
comment: `user {name} {value, plural, | ||
=0 {does not have any comments} | ||
=1 {has # comment} | ||
other {has # comments} | ||
}`, | ||
HTMLComment: `user <b style='font-weight: bold'>{name} </b> {value, plural, | ||
=0 {does not have <i style='font-style: italic'>any</i> comments} | ||
=1 {has <i style='font-style: italic'>#</i> comment} | ||
other {has <i style='font-style: italic'>#</i> comments} | ||
}`, | ||
nestedDateComment: `user {name} {value, plural, | ||
=0 {does not have any comments} | ||
=1 {has # comment} | ||
other {has # comments} | ||
} as of {date}`, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
locale: 'fr', | ||
messages: { | ||
siteTitle: 'MERN blog de démarrage', | ||
addPost: 'Ajouter Poster', | ||
switchLanguage: 'Changer de langue', | ||
twitterMessage: 'Nous sommes sur Twitter', | ||
by: 'Par', | ||
deletePost: 'Supprimer le message', | ||
createNewPost: 'Créer un nouveau message', | ||
authorName: 'Nom de l\'auteur', | ||
postTitle: 'Titre de l\'article', | ||
postContent: 'Contenu après', | ||
submit: 'Soumettre', | ||
comment: `user {name} {value, plural, | ||
=0 {does not have any comments} | ||
=1 {has # comment} | ||
other {has # comments} | ||
} (in real app this would be translated to French)`, | ||
HTMLComment: `user <b style='font-weight: bold'>{name} </b> {value, plural, | ||
=0 {does not have <i style='font-style: italic'>any</i> comments} | ||
=1 {has <i style='font-style: italic'>#</i> comment} | ||
other {has <i style='font-style: italic'>#</i> comments} | ||
} (in real app this would be translated to French)`, | ||
nestedDateComment: `user {name} {value, plural, | ||
=0 {does not have any comments} | ||
=1 {has # comment} | ||
other {has # comments} | ||
} as of {date} (in real app this would be translated to French)`, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// list of available languages | ||
export const enabledLanguages = [ | ||
'en', | ||
'fr', | ||
]; | ||
|
||
// this object will have language-specific data added to it which will be placed in the state when that language is active | ||
// if localization data get to big, stop importing in all languages and switch to using API requests to load upon switching languages | ||
export const localizationData = {}; | ||
|
||
// here you bring in 'intl' browser polyfill and language-specific polyfills | ||
// (needed as safari doesn't have native intl: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) | ||
// as well as react-intl's language-specific data | ||
// be sure to use static imports for language or else every language will be included in your build (adds ~800 kb) | ||
import { addLocaleData } from 'react-intl'; | ||
|
||
// need Intl polyfill, Intl not supported in Safari | ||
import Intl from 'intl'; | ||
global.Intl = Intl; | ||
|
||
// use this to allow nested messages, taken from docs: | ||
// https://github.com/yahoo/react-intl/wiki/Upgrade-Guide#flatten-messages-object | ||
function flattenMessages(nestedMessages = {}, prefix = '') { | ||
return Object.keys(nestedMessages).reduce((messages, key) => { | ||
const value = nestedMessages[key]; | ||
const prefixedKey = prefix ? `${prefix}.${key}` : key; | ||
|
||
if (typeof value === 'string') { | ||
messages[prefixedKey] = value; // eslint-disable-line no-param-reassign | ||
} else { | ||
Object.assign(messages, flattenMessages(value, prefixedKey)); | ||
} | ||
|
||
return messages; | ||
}, {}); | ||
} | ||
|
||
// bring in intl polyfill, react-intl, and app-specific language data | ||
import 'intl/locale-data/jsonp/en'; | ||
import en from 'react-intl/locale-data/en'; | ||
import enData from './localizationData/en'; | ||
addLocaleData(en); | ||
localizationData.en = enData; | ||
localizationData.en.messages = flattenMessages(localizationData.en.messages); | ||
|
||
import 'intl/locale-data/jsonp/fr'; | ||
import fr from 'react-intl/locale-data/fr'; | ||
import frData from './localizationData/fr'; | ||
addLocaleData(fr); | ||
localizationData.fr = frData; | ||
localizationData.fr.messages = flattenMessages(localizationData.fr.messages); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { localizationData } from '../../../Intl/setup'; | ||
|
||
// Export Constants | ||
export const SWITCH_LANGUAGE = 'SWITCH_LANGUAGE'; | ||
|
||
export function switchLanguage(newLang) { | ||
return { | ||
type: SWITCH_LANGUAGE, | ||
...localizationData[newLang], | ||
}; | ||
} |
Oops, something went wrong.