From 16b3db9483ffb53c7651821c308de643f47a8249 Mon Sep 17 00:00:00 2001 From: Debabrata Nayak Date: Fri, 18 Oct 2019 17:34:12 +0530 Subject: [PATCH] first commit --- README.md | 67 ++-------------------------------------------- package.json | 1 + src/App.css | 20 ++------------ src/App.js | 75 +++++++++++++++++++++++++++++++++++++++------------- src/logo.svg | 1 - yarn.lock | 46 +++++++++++++++++++++++++++++--- 6 files changed, 105 insertions(+), 105 deletions(-) delete mode 100644 src/logo.svg diff --git a/README.md b/README.md index 89b278a..656d5a6 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,5 @@ -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +This project is bootsrapped with create-react-app and demonstrates how to do localization for multiple different languages using [React Intl](https://github.com/formatjs/react-intl) (v2.8.0) -## Available Scripts - -In the project directory, you can run: +## Run project ### `yarn start` - -Runs the app in the development mode.
-Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.
-You will also see any lint errors in the console. - -### `yarn test` - -Launches the test runner in the interactive watch mode.
-See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `yarn build` - -Builds the app for production to the `build` folder.
-It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.
-Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `yarn eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting - -### Analyzing the Bundle Size - -This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size - -### Making a Progressive Web App - -This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app - -### Advanced Configuration - -This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration - -### Deployment - -This section has moved here: https://facebook.github.io/create-react-app/docs/deployment - -### `yarn build` fails to minify - -This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify diff --git a/package.json b/package.json index ac0012a..0d07a02 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "dependencies": { "react": "^16.10.2", "react-dom": "^16.10.2", + "react-intl": "2.8.0", "react-scripts": "3.2.0" }, "scripts": { diff --git a/src/App.css b/src/App.css index afc3885..974d07b 100644 --- a/src/App.css +++ b/src/App.css @@ -1,22 +1,6 @@ .App { text-align: center; } - -.App-logo { - height: 40vmin; -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #09d3ac; +footer{ + margin-top: 24px; } diff --git a/src/App.js b/src/App.js index ce9cbd2..22c0f31 100644 --- a/src/App.js +++ b/src/App.js @@ -1,25 +1,64 @@ -import React from 'react'; -import logo from './logo.svg'; +import React, { useState } from 'react'; import './App.css'; +import { IntlProvider, FormattedMessage } from 'react-intl'; +const en = require('react-intl/locale-data/en'); +const zh = require('react-intl/locale-data/zh'); +const ru = require('react-intl/locale-data/ru'); +const fr = require('react-intl/locale-data/fr'); +const addLocaleData = require('react-intl').addLocaleData; +addLocaleData(en); +addLocaleData(zh); +addLocaleData(ru); +addLocaleData(fr); + + +const defaultLocale = localStorage['locale'] ? localStorage['locale'] : 'en'; // English is default locale if none is set +const localeList = [ + { name: 'English', code: 'en', lang: 'English' }, + { name: '中文', code: 'zh', lang: 'Chinese' }, + { name: 'русский', code: 'ru', lang: 'Russian' }, + { name: 'Française', code: 'fr', lang: 'French' } +]; +const messages={ + en: { + 'dashboard.header': `Localization in Create React App`, + 'dashboard.footer': 'Love you 3000' + }, + zh: { + 'dashboard.header': `Create React App中的本地化`, + 'dashboard.footer': `爱你3000` + }, + ru: { + 'dashboard.header': `Локализация в приложении Создать React`, + 'dashboard.footer': `Люблю тебя 3000` + }, + fr: { + 'dashboard.header': `Localisation dans l'application Create React`, + 'dashboard.footer': `Je t'aime 3000` + }, +} function App() { + const [currentLocale, setCurrentLocale] = useState(defaultLocale); + const onChangeLanguage = (e) => { + console.log("selected locale",e.target.value); + setCurrentLocale(e.target.value); + localStorage.setItem('locale',currentLocale) + } return ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
-
+ +
+

+ +
+
+
); } diff --git a/src/logo.svg b/src/logo.svg deleted file mode 100644 index 2e5df0d..0000000 --- a/src/logo.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 66e7583..8711c96 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4734,6 +4734,11 @@ hmac-drbg@^1.0.0: minimalistic-assert "^1.0.0" minimalistic-crypto-utils "^1.0.1" +hoist-non-react-statics@^2.5.5: + version "2.5.5" + resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47" + integrity sha512-rqcy4pJo55FTTLWt+bU8ukscqHeE/e9KWvsOW2b/a3afxQZhwkQdT1rPPCJ0rYXdj4vNcasY8zHTH+jF/qStxw== + hosted-git-info@^2.1.4: version "2.8.4" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.4.tgz#44119abaf4bc64692a16ace34700fed9c03e2546" @@ -5073,7 +5078,31 @@ internal-ip@^4.2.0: default-gateway "^4.2.0" ipaddr.js "^1.9.0" -invariant@^2.2.2, invariant@^2.2.4: +intl-format-cache@^2.0.5: + version "2.2.9" + resolved "https://registry.yarnpkg.com/intl-format-cache/-/intl-format-cache-2.2.9.tgz#fb560de20c549cda20b569cf1ffb6dc62b5b93b4" + integrity sha512-Zv/u8wRpekckv0cLkwpVdABYST4hZNTDaX7reFetrYTJwxExR2VyTqQm+l0WmL0Qo8Mjb9Tf33qnfj0T7pjxdQ== + +intl-messageformat-parser@1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/intl-messageformat-parser/-/intl-messageformat-parser-1.4.0.tgz#b43d45a97468cadbe44331d74bb1e8dea44fc075" + integrity sha1-tD1FqXRoytvkQzHXS7Ho3qRPwHU= + +intl-messageformat@^2.0.0, intl-messageformat@^2.1.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/intl-messageformat/-/intl-messageformat-2.2.0.tgz#345bcd46de630b7683330c2e52177ff5eab484fc" + integrity sha1-NFvNRt5jC3aDMwwuUhd/9eq0hPw= + dependencies: + intl-messageformat-parser "1.4.0" + +intl-relativeformat@^2.1.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/intl-relativeformat/-/intl-relativeformat-2.2.0.tgz#6aca95d019ec8d30b6c5653b6629f9983ea5b6c5" + integrity sha512-4bV/7kSKaPEmu6ArxXf9xjv1ny74Zkwuey8Pm01NH4zggPP7JHwg2STk8Y3JdspCKRDriwIyLRfEXnj2ZLr4Bw== + dependencies: + intl-messageformat "^2.0.0" + +invariant@^2.1.1, invariant@^2.2.2, invariant@^2.2.4: version "2.2.4" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6" integrity sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA== @@ -8333,7 +8362,7 @@ react-dev-utils@^9.1.0: strip-ansi "5.2.0" text-table "0.2.0" -react-dom@16.10.2: +react-dom@^16.10.2: version "16.10.2" resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.10.2.tgz#4840bce5409176bc3a1f2bd8cb10b92db452fda6" integrity sha512-kWGDcH3ItJK4+6Pl9DZB16BXYAZyrYQItU4OMy0jAkv5aNqc+mAKb4TpFtAteI6TJZu+9ZlNhaeNQSVQDHJzkw== @@ -8348,6 +8377,17 @@ react-error-overlay@^6.0.3: resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.3.tgz#c378c4b0a21e88b2e159a3e62b2f531fd63bf60d" integrity sha512-bOUvMWFQVk5oz8Ded9Xb7WVdEi3QGLC8tH7HmYP0Fdp4Bn3qw0tRFmr5TW6mvahzvmrK4a6bqWGfCevBflP+Xw== +react-intl@2.8.0: + version "2.8.0" + resolved "https://registry.yarnpkg.com/react-intl/-/react-intl-2.8.0.tgz#20b0c1f01d1292427768aa8ec9e51ab7e36503ba" + integrity sha512-1cSasNkHxZOXYYhms9Q1tSEWF8AWZQNq3nPLB/j8mYV0ZTSt2DhGQXHfKrKQMu4cgj9J1Crqg7xFPICTBgzqtQ== + dependencies: + hoist-non-react-statics "^2.5.5" + intl-format-cache "^2.0.5" + intl-messageformat "^2.1.0" + intl-relativeformat "^2.1.0" + invariant "^2.1.1" + react-is@^16.8.1, react-is@^16.8.4: version "16.9.0" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.9.0.tgz#21ca9561399aad0ff1a7701c01683e8ca981edcb" @@ -8414,7 +8454,7 @@ react-scripts@3.2.0: optionalDependencies: fsevents "2.0.7" -react@16.10.2: +react@^16.10.2: version "16.10.2" resolved "https://registry.yarnpkg.com/react/-/react-16.10.2.tgz#a5ede5cdd5c536f745173c8da47bda64797a4cf0" integrity sha512-MFVIq0DpIhrHFyqLU0S3+4dIcBhhOvBE8bJ/5kHPVOVaGdo0KuiQzpcjCPsf585WvhypqtrMILyoE2th6dT+Lw==