How amsul/react-translated works
Project Type and Purpose
• This is an open-source React library (“react-translated”) that provides a dead-simple translation layer for React DOM and React Native apps.
• Purpose: supply a <Provider> for context, a <Translate> component (for JSX output) and a Translator helper (for string output), plus interpolation and component-in-text features.
a. Host Application
• React DOM App
• React Native App
• User-supplied translation file (JSON or JS mapping keys to language dictionaries/functions)
b. Library Core (under lib/)
• Provider (context provider for language & translation data)
• Translate component (subscribes to context, renders translated JSX)
• Translator component (provides translate function for string use cases)
• Renderer / createRenderer (internal rendering pipeline)
• createSubscribe / createTranslate (subscription and caching logic)
• getTranslated (core logic that picks the correct translation string/function & applies data, interpolation, Markdown–style or component replacements)
• Utilities & constants (helper functions, default configs)
c. Bundler & Build
• rollup.config.js (defines input lib/index.js, output formats)
• .babelrc / .eslintrc / flowconfig (tooling configs)
d. Examples (just for reference—external to core)
• examples/react-dom (demonstrates usage in a web app)
• examples/react-native (demonstrates usage in a native mobile app)
Relationships and Data Flow
1. Host App imports <Provider language translation> from react-translated
2. Provider places language & translation into React Context
3. Host App renders <Translate> or <Translator> deeper in component tree
4. Translate/Translator uses createSubscribe to listen to context changes
5. On render, Translate calls getTranslated(text, data, renderMap, language, translation)
• If translation value is a function, it is invoked with data to select correct form (pluralization, grammar)
• Interpolation and Markdown-style parsing happen here
• RenderMap components get plugged in
6. createRenderer takes the result and outputs React elements or plain string
7. React reconciler (React DOM or React Native) actually paints the UI
Architectural Patterns & Principles
• Context-driven (React Context) for global language & translations
• Publisher–subscriber (createSubscribe) to re-render only impacted <Translate> components when language changes
• Functional composition: splitting “translate retrieval” (getTranslated) from “render” (renderer)
• Single responsibility: Provider vs. Translate vs. Translator vs. core logic
• Pluggable interpolation: renderMap for custom component injections
Elements
• A “Host App” boundary showing React DOM vs. React Native integration
• Data flow: translation file → Provider → Translate → getTranslated → renderer → React host
• Dependencies: Translate depends on getTranslated and context
• Events: language change triggers providers updates → re-subscribe → re-render
• Host Application (blue)
• Library Core (green)
• User Data/Config (yellow)
• React Host (orange)
• Rectangles for modules/components