Understanding and Implementing Schemas in Python

Understanding and Implementing Schemas in Python Introduction In the world of programming, particularly in the context of data management and validation, schemas play a vital role. A schema is essentially a blueprint or a predefined structure that defines the expected format, data types, and constraints for a given data entity. In this blog, we will delve into the concept of schemas in Python, exploring what they are, why they are important, and how you can implement them in your projects. What is a Schema? A schema serves as a contract between different components of a system, ensuring that data is consistent, valid, and well-structured. It defines the rules for how data should be organized, what fields it should contain, and what types of values those fields can hold. In essence, a schema acts as a set of rules that data must adhere to in order to be considered valid. Why Are Schemas Important? Data Validation: Schemas provide a way to validate incoming data. When data is received o

Localization in React Js

Localization in React Js using react-localization package.

In this post, we will learn about Localization in React Js. Now a day, Localization is very important and basic requirement in every web based application. Following source code will help you, creating a multilingual Web application in React Js.

Note: Use Double Click for Radio Boxes.

localization-in-react-js
Localization in React Js

Installation:

npm install --save react-localization

Folder Structure:


folderstructure


Sample Code

A) index.js

import React from "react";
import ReactDOM from "react-dom";

import App from "./App";

ReactDOM.render(<App />document.getElementById("root"));

B) App.js ( Class Component )

import React, { Component } from 'react';
import labels from './Local/localize';
import './style.css';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      language: 'english',
    };
  }

  handleChangeLanguage = (e=> {
    e.preventDefault();
    let lang = e.target.value;
    this.setState((prevState=> ({
      language: lang,
    }));
  };
  render() {
    labels.setLanguage(this.state.language);
    return (
      <div id="mainDiv">
        <div id="navbar">
          <div>{labels.Home}</div>
          <div>{labels.AboutUs}</div>
          <div>{labels.ContactUs}</div>
        </div>
        <br />
        <div id="leftSide">
          <div id="data">{labels.FirstName} </div>
          <div id="input">
            <input type="text" />
          </div>{' '}
          <br />
          <div id="data">{labels.LastName} </div>{' '}
          <div id="input">
            <input type="text" />
          </div>
          <br />
          <div id="data">{labels.Addresss}</div>{' '}
          <div id="input">
            <input type="text" />
          </div>
          <br />
          <button type="submit"> {labels.Save} </button>
          <button type="submit"> {labels.Cancel} </button> <br />
        </div>
        <div id="rightSide">
          <div id="radio">
            <input
              type="radio"
              name="lan"
              value="english"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> English</span>
            <br />
            <input
              type="radio"
              name="lan"
              value="spanish"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> Spanish</span>
            <br />
            <input
              type="radio"
              name="lan"
              value="french"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> French</span>
            <br />
            <input
              type="radio"
              name="lan"
              value="italian"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> Italian</span>
            <br />
            <input
              type="radio"
              name="lan"
              value="hindi"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> Hindi</span>
            <br />
            <input
              type="radio"
              name="lan"
              value="german"
              onChange={this.handleChangeLanguage}
            ></input>
            <span> German</span>
            <br />
          </div>
        </div>
      </div>
    );
  }
}

export default App;

C) english.js

let english = {
  FirstName: 'First Name',
  LastName: 'Last Name',
  Addresss: 'Address',
  Save: 'Save',
  Cancel: 'Cancel',
  Home: 'Home',
  AboutUs: 'About Us',
  ContactUs: 'Contact Us',
}

export default english;

D) french.js

let french = {
  FirstName: 'Prénom',
  LastName: 'Nom',
  Addresss: 'Adresse',
  Save: 'Sauvegarder',
  Cancel: 'Annuler',
  Home: 'Domicile',
  AboutUs: 'Qui sommes-nous',
  ContactUs: 'Contactez-nous',
};

export default french;

E) german.js

let german = {
  FirstName: 'Vorname',
  LastName: 'Nachname',
  Addresss: 'Adresse',
  Save: 'Retten',
  Cancel: 'Abbrechen',
  Home: 'Heim',
  AboutUs: 'Über uns',
  ContactUs: 'Kontaktieren Sie uns',
};

export default german;

F) hindi.js

let hindi = {
  FirstName: 'फर्स्ट नेम',
  LastName: 'लास्ट नेम',
  Addresss: 'एड्रेस',
  Save: 'सेव',
  Cancel: 'कैंसिल ',
  Home: 'होम',
  AboutUs: 'अबाउट अस',
  ContactUs: 'कांटेक्ट अस',
};

export default hindi;

G) italian.js

let italian = {
  FirstName: 'Nome di battesimo',
  LastName: 'Cognome',
  Addresss: 'Indirizzo',
  Save: 'Salvare',
  Cancel: 'Annulla',
  Home: 'Casa',
  AboutUs: 'Chi Siamo',
  ContactUs: 'Contattaci',
};

export default italian;

H) spanish.js

let spanish = {
  FirstName: 'Nombre',
  LastName: 'Apellido',
  Addresss: 'Dirección',
  Save: 'Salvar',
  Cancel: 'Cancelar',
  Home: 'Hogar',
  AboutUs: 'Sobre nosotros',
  ContactUs: 'Contáctenos',
};

export default spanish;

I) localize.js

import LocalizedStrings from 'react-localization';
import english from './english';
import french from './french';
import spanish from './spanish';
import italian from './italian';
import hindi from './hindi';
import german from './german';

let labels = new LocalizedStrings({
  english: english,
  french: french,
  spanish: spanish,
  italian: italian,
  hindi: hindi,
  german: german,
});

export default labels;

J) styles.css

#leftSide {
  border1px solid black;
  width350px;
  floatleft;
  margin1px;
  height180px;
  background-colorrgb(187174174);
  colorblack;
  font-weightbold;
}

#rightSide {
  border1px solid black;
  width200px;
  height180px;
  positionabsolute;
  left360px;
  margin2px;
  background-colorrgb(187174174);
  colorblack;
  font-weightbold;
}

#mainDiv {
  positionrelative;
}

#data {
  margin5px;
  width150px;
  floatleft;
}

#input {
  margin5px;
}

button {
  margin-left20px;
  width100px;
  height25px;
  border-radius10px;
  background-colorrgb(777);
  colorrgb(2481647);
}

#navbar {
  border1px solid black;
  width560px;
  height25px;
  displayflex;
  line-height25px;
  background-colorblack;
  colororange;
}

#navbar div {
  margin-right25px;
  margin-left6px;
}

K) Package.json

{
  "name""react",
  "version""0.0.0",
  "private"true,
  "dependencies": {
    "react""^17.0.2",
    "react-dom""^17.0.2",
    "react-localization""^1.0.17"
  },
  "scripts": {
    "start""react-scripts start",
    "build""react-scripts build",
    "test""react-scripts test --env=jsdom",
    "eject""react-scripts eject"
  },
  "devDependencies": {
    "react-scripts""latest"
  }
}

Functional Component: 

Just replace below App.js file for functional component,  use rest of the files as it is.

App.js

import React, { useState } from 'react';
import labels from './Local/localize';
import './style.css';

function App(props) {
  const [langsetLang] = useState('english');
  labels.setLanguage(lang);

  const handleChangeLanguage = (e=> {
    e.preventDefault();
    let lan = e.target.value;
    setLang(lan);
  };

  return (
    <div id="mainDiv">
      <div id="navbar">
        <div>{labels.Home}</div>
        <div>{labels.AboutUs}</div>
        <div>{labels.ContactUs}</div>
      </div>
      <br />
      <div id="leftSide">
        <div id="data">{labels.FirstName} </div>
        <div id="input">
          <input type="text" />
        </div>{' '}
        <br />
        <div id="data">{labels.LastName} </div>{' '}
        <div id="input">
          <input type="text" />
        </div>
        <br />
        <div id="data">{labels.Addresss}</div>{' '}
        <div id="input">
          <input type="text" />
        </div>
        <br />
        <button type="submit"> {labels.Save} </button>
        <button type="submit"> {labels.Cancel} </button> <br />
      </div>
      <div id="rightSide">
        <div id="radio">
          <input
            type="radio"
            name="lan"
            value="english"
            onChange={handleChangeLanguage}
          ></input>
          <span> English</span>
          <br />
          <input
            type="radio"
            name="lan"
            value="spanish"
            onChange={handleChangeLanguage}
          ></input>
          <span> Spanish</span>
          <br />
          <input
            type="radio"
            name="lan"
            value="french"
            onChange={handleChangeLanguage}
          ></input>
          <span> French</span>
          <br />
          <input
            type="radio"
            name="lan"
            value="italian"
            onChange={handleChangeLanguage}
          ></input>
          <span> Italian</span>
          <br />
          <input
            type="radio"
            name="lan"
            value="hindi"
            onChange={handleChangeLanguage}
          ></input>
          <span> Hindi</span>
          <br />
          <input
            type="radio"
            name="lan"
            value="german"
            onChange={handleChangeLanguage}
          ></input>
          <span> German</span>
          <br />
        </div>
      </div>
    </div>
  );
}

export default App;


Happy Learning!! Happy Coding!!

Comments

  1. Nice blog, thank you so much for sharing...

    ReplyDelete
  2. Nice blog about localization in react, thanks for sharing this post

    ReplyDelete

Popular posts from this blog

useNavigate and useLocation hooks react-router-dom-v6

How to implement error boundaries in React Js

Pass data from child component to its parent component in React Js

Create a Shopping Item App using React Js and Xstate

How to fetch data from an API using fetch() method in React Js

How to fetch data using Axios Http Get Request in React Js?

Routing in React using React-Router Version 6

Create a custom calendar in React Js | Interview Question

Environment Setup and Installation for React Js Application