Repository: synacor/preact-i18n Branch: master Commit: 28ff35cbdbee Files: 25 Total size: 57.1 KB Directory structure: gitextract_qzqk6_j4/ ├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── karma.conf.js ├── package.json ├── rollup.config.js ├── src/ │ ├── components/ │ │ ├── highlight-i18n.js │ │ ├── intl-provider.js │ │ ├── localizer.js │ │ ├── markup-text.js │ │ ├── text.js │ │ └── with-text.js │ ├── index.js │ ├── intl.js │ └── lib/ │ ├── template.js │ ├── translate-mapping.js │ ├── translate.js │ └── util.js └── test/ ├── .eslintrc ├── index.js └── lib/ ├── template.js ├── translate.js └── util.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,.*rc,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false ================================================ FILE: .gitignore ================================================ /docs /dist /test-reports /node_modules /npm-debug.log .DS_Store ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - stable ================================================ FILE: LICENSE ================================================ Copyright (c) 2017, Synacor, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================ FILE: README.md ================================================ # preact-i18n 🌎 [![npm](https://img.shields.io/npm/v/preact-i18n.svg?style=flat)](https://npm.im/preact-i18n) [![travis](https://travis-ci.org/synacor/preact-i18n.svg?branch=master)](https://travis-ci.org/synacor/preact-i18n) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsynacor%2Fpreact-i18n.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fsynacor%2Fpreact-i18n?ref=badge_shield) Usage example **Simple localization for Preact.** - Tiny: about 1.3kb gzipped - Supports dictionary and key scopes/namespaces while maintaining a global dictionary - Supports nested dictionaries: - Wrap your component in a default dictionary and scope key - Wrap it again later on (in an app!) to override the defaults - Supports pluralization of strings using nested objects. - Supports template `{{fields}}` in definition values - Has a companion [ESLint plugin](https://www.npmjs.com/package/eslint-plugin-preact-i18n) to help catch bugs early * * * - [Installation](#installation) - [Getting Started](#getting-started) - [Fallback Text](#fallback-text) - [Pluralization and Templating](#pluralization-and-templating) - [ESLint Plugin](#eslint-plugin) - [API](#api) ## Preact Version Support By default, the `master` branch of this repo supports preact 9 and below, and is published in normal patch/minor/major releases to the `latest` tag in npm. Support for preact X (versions 10+ of preact) is handled in the `preactX` branch and are always published to the `preactx` tag in npm. When preact X obtains widespread adoption, the `master` branch of this project will support preact X and a new major version under `latest` tag will be published to in npm. ## Installation ```sh npm install --save preact-i18n # For TypeScript Definitions npm install --save-dev @types/preact-i18n ``` ## Getting Started 1. Create a definition. Typically JSON files, we'll call ours `fr.json`: ```json { "news": { "title": "Nouvelles du Monde", "totalStories": { "none": "Aucun article", "one": "Un article", "many": "{{count}} articles" } } } ``` 2. Expose the definition to your whole app via ``: ```js import { IntlProvider } from 'preact-i18n'; import definition from './fr.json'; render( ); ``` 3. Use `` to translate string literals: ```js import { Text } from 'preact-i18n'; // Assume the "stories" prop is a list of news stories. const App = ({ stories=[] }) => (

{/* Default fallback text example: */} World News

{/* Pluralization example: */}
); ``` That's it! ### Fallback Text Rendering our example app with an empty definition _(or without the Provider)_ will attempt to use any text contained within `..` as fallback text. In our example, this would mean rendering without a definition for `news.title` would produce `

World News

`. If we provide a definition that has a `title` key inside a `news` object, that value will be rendered instead. ### Pluralization and Templating In our example, `