3.4.10 • Published 11 months ago

dk-localize v3.4.10

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Library for injecting texts into templates

coverage npm license size

!WARNING
It's fine if you use this library from NPM package with a static versioning in case you want it for some pet-project or to test it's capabilities.

But for production use it's strongly recommended to create a fork, because I do not write Changelogs and may break / add some functionality without notice.

Currently suits English-only projects

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.4.10

11 months ago

3.4.5

1 year ago

3.4.4

1 year ago

3.4.3

1 year ago

3.4.2

1 year ago

3.4.0

1 year ago

3.4.1

1 year ago

3.3.9

1 year ago

3.3.8

1 year ago

3.3.6

1 year ago

3.3.1

1 year ago

3.3.0

1 year ago

3.3.5

1 year ago

3.3.4

1 year ago

3.3.3

1 year ago

3.3.2

1 year ago

3.2.24

1 year ago

3.2.23

1 year ago

3.2.26

1 year ago

3.2.25

1 year ago

3.2.28

1 year ago

3.2.27

1 year ago

3.2.20

1 year ago

3.2.22

1 year ago

3.2.21

1 year ago

3.2.17

1 year ago

3.2.16

1 year ago

3.2.19

1 year ago

3.2.18

1 year ago

3.2.15

1 year ago

3.2.9

2 years ago

3.2.13

2 years ago

3.2.12

2 years ago

3.2.14

2 years ago

3.2.11

2 years ago

3.2.10

2 years ago

3.2.8

2 years ago

3.2.7

2 years ago

3.2.6

2 years ago

3.2.6-alpha.0

2 years ago

3.2.2

2 years ago

3.2.3

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.43

2 years ago

3.1.42

2 years ago

3.1.41

2 years ago

3.1.40

2 years ago

3.1.39

2 years ago

3.1.38

2 years ago

3.1.16

2 years ago

3.1.15

2 years ago

3.1.18

2 years ago

3.1.17

2 years ago

3.1.23

2 years ago

3.1.22

2 years ago

3.1.25

2 years ago

3.1.24

2 years ago

3.1.27

2 years ago

3.1.26

2 years ago

3.1.29

2 years ago

3.1.28

2 years ago

3.1.21

2 years ago

3.1.20

2 years ago

3.1.19

2 years ago

3.1.34

2 years ago

3.1.33

2 years ago

3.1.36

2 years ago

3.1.35

2 years ago

3.1.37

2 years ago

3.1.30

2 years ago

3.1.32

2 years ago

3.1.31

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.14

2 years ago

3.1.13

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.0.18

2 years ago

3.0.19

2 years ago

3.0.9

2 years ago

3.0.4

2 years ago

3.1.2

2 years ago

3.0.13

2 years ago

3.1.1

2 years ago

3.0.10

2 years ago

3.1.0

2 years ago

3.0.11

2 years ago

3.1.7

2 years ago

3.0.16

2 years ago

3.0.8

2 years ago

3.1.6

2 years ago

3.0.17

2 years ago

3.0.7

2 years ago

3.1.5

2 years ago

3.0.14

2 years ago

3.0.6

2 years ago

3.1.4

2 years ago

3.0.15

2 years ago

2.0.2

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago