3.2.14 • Published 29 days ago

dk-localize v3.2.14

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

Library for injecting texts into templates

coverage npm license

Currently suits English-only projects. Features are limited, development is in progress in accordance to my needs

Installation

Add dk-localize to package.json and install.

Usage

  1. Allow usage of __dirname in you builder. For Webpack it is { node: { __dirname: true } }

  2. Attach getLn to the place where localized text will be located, ex.:

import { getLn as getLnNotBound, TypeTranslations } from 'dk-localize';

class GlobalStore {
  ln: TypeTranslations = {};
};

const store = new GlobalStore();

const getLn = getLnNotBound.bind(null, store.ln);
  1. Pass getLn to components (ex. in React by Context)
import { createContext } from 'react';
import { render } from 'react-dom';

const StoreContext = createContext(undefined);

render(
  <StoreContext.Provider value={{ getLn }}>
    <App />
  </StoreContext.Provider>,
  document.getElementById('app')
)
  1. Create file with default texts, ex. src/page/messages.ts
import { wrapMessages } from 'dk-localize';

export const messages = wrapMessages(__dirname, {
  title: 'Hello {user}',
});
  1. Use in components, ex. in React src/page/Page.tsx
import { Component, ContextType } from 'react';
import { messages } from './messages';
import { StoreContext } from '/StoreContext';

type TypeContext = ContextType<typeof StoreContext>;

export class PushMessagesList extends Component {
  static context: TypeContext;
  static contextType = StoreContext;
  declare context: TypeContext;
  
  render() {
      const { getLn } = this.context;
      
      return <>{getLn(messages.title, { user: 'John' })}</>
  }
}

Features

  • wrapMessages creates uniq names for every key by __dirname, so in the example above it will be src/page__title

Coming features:

  • preparation of JSON files that can be uploaded to localization system

Plugins

Dynamic variables

Dynamic value must follow in brackets, supported value types are string | number, ex.

// mesages.ts
import { wrapMessages } from 'dk-localize';

export const messages = wrapMessages(__dirname, {
  title: 'Name: {firstName}, age: {age}',
});

// component
getLn(messages.title, { firstName: 'John', age: 25 }) // 'Name: John, age: 25'

Plurals

Plural value must follow in brackets, supported value type is number, ex.

// mesages.ts
import { wrapMessages } from 'dk-localize';

export const messages = wrapMessages(__dirname, {
  title: '{count: One,Many} {count: apple,apples}',
});

// component
getLn(messages.title, { count: 1 }) // 'One apple'
getLn(messages.title, { count: 2 }) // 'Many apples'

Syntax is {param: textForOne,textForMany}. Extra spaces and special symbols for texts are currently not supported, only [a-zA-Z0-9].

3.2.9

29 days ago

3.2.13

29 days ago

3.2.12

29 days ago

3.2.14

29 days ago

3.2.11

29 days ago

3.2.10

29 days ago

3.2.8

1 month ago

3.2.7

1 month ago

3.2.6

1 month ago

3.2.6-alpha.0

1 month ago

3.2.2

1 month ago

3.2.3

1 month ago

3.2.1

1 month ago

3.2.0

1 month ago

3.1.43

3 months ago

3.1.42

3 months ago

3.1.41

3 months ago

3.1.40

3 months ago

3.1.39

3 months ago

3.1.38

4 months ago

3.1.16

10 months ago

3.1.15

10 months ago

3.1.18

10 months ago

3.1.17

10 months ago

3.1.23

9 months ago

3.1.22

10 months ago

3.1.25

9 months ago

3.1.24

9 months ago

3.1.27

7 months ago

3.1.26

9 months ago

3.1.29

7 months ago

3.1.28

7 months ago

3.1.21

10 months ago

3.1.20

10 months ago

3.1.19

10 months ago

3.1.34

6 months ago

3.1.33

6 months ago

3.1.36

5 months ago

3.1.35

6 months ago

3.1.37

5 months ago

3.1.30

7 months ago

3.1.32

6 months ago

3.1.31

6 months ago

3.1.12

11 months ago

3.1.11

11 months ago

3.1.14

11 months ago

3.1.13

11 months ago

3.1.9

11 months ago

3.1.8

11 months ago

3.0.18

11 months ago

3.0.19

11 months ago

3.0.9

11 months ago

3.0.4

11 months ago

3.1.2

11 months ago

3.0.13

11 months ago

3.1.1

11 months ago

3.0.10

11 months ago

3.1.0

11 months ago

3.0.11

11 months ago

3.1.7

11 months ago

3.0.16

11 months ago

3.0.8

11 months ago

3.1.6

11 months ago

3.0.17

11 months ago

3.0.7

11 months ago

3.1.5

11 months ago

3.0.14

11 months ago

3.0.6

11 months ago

3.1.4

11 months ago

3.0.15

11 months ago

2.0.2

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago