diff --git a/package-lock.json b/package-lock.json index 5a26005..88a07a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5938,6 +5938,12 @@ "xml-char-classes": "https://registry.npmjs.org/xml-char-classes/-/xml-char-classes-1.0.0.tgz" } }, + "ncp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", + "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=", + "dev": true + }, "negotiator": { "version": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk=", diff --git a/package.json b/package.json index 80494d4..57d8687 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "./lib/index.js", "module": "./es/index.js", "jsnext:main": "./es/index.js", + "typings": "lib/index.d.ts", "files": [ "*.md", "es", @@ -12,12 +13,13 @@ "dist" ], "scripts": { - "build": "npm run build:commonjs && npm run build:es && npm run build:flow && npm run build:umd && npm run build:umd:min", + "build": "npm run build:commonjs && npm run build:es && npm run build:flow && npm run build:umd && npm run build:umd:min && npm run copy:ts", "build:commonjs": "rimraf lib && cross-env BABEL_ENV=commonjs babel ./src -d lib", "build:es": "rimraf es && cross-env BABEL_ENV=es babel ./src -d es", "build:umd": "rimraf dist && webpack --env.dev --output-filename dist/ReactLocalizeRedux.js", "build:umd:min": "webpack --env.prod --output-filename dist/ReactLocalizeRedux.min.js", "build:flow": "flow-copy-source ./src lib && flow-copy-source ./src es", + "copy:ts": "ncp ./src/index.d.ts ./lib/index.d.ts && ncp ./src/index.d.ts ./es/index.d.ts", "coverage": "jest --coverage", "prepublish": "npm run build", "start": "webpack-dev-server --config examples/webpack.config.babel.js --content-base examples --inline --open", @@ -79,6 +81,7 @@ "html-webpack-plugin": "^2.24.1", "jest": "^20.0.4", "json-loader": "^0.5.4", + "ncp": "^2.0.0", "progress-bar-webpack-plugin": "^1.9.1", "react": "^15.6.1", "react-addons-test-utils": "^15.6.0", diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..2ec71fe --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,122 @@ +import { ReactElement } from 'react'; +import { Selector } from 'reselect'; +import { ComponentClass } from 'react-redux'; + +export as namespace ReactLocalizeRedux; + +export interface Language { + code: string; + active: boolean; +} + +export interface Translations { + [key: string]: string[]; +} + +export interface Options { + renderInnerHtml?: boolean; + defaultLanguage?: string; +} + +export interface LocaleState { + languages: Language[]; + translations: Translations; + options: Options; +} + +export interface TranslatedLanguage { + [key: string]: string; +} + +export type LocalizedElement = ReactElement<'span'>|string; + +export interface LocalizedElementMap { + [key: string]: LocalizedElement; +} + +export interface TranslatePlaceholderData { + [key: string]: string|number; +} + +export type TranslateValue = string|string[]; + +interface BaseAction { + type: T; + payload: P; +} + +export type Translate = (value: TranslateValue, data: TranslatePlaceholderData, options?: Options) => LocalizedElement|LocalizedElementMap; + +type InitializePayload = { + languageCodes: string[], + options?: Options +}; + +type AddTranslationPayload = { + translation: Object +}; + +type AddTranslationForLanguagePayload = { + translation: Object, + language: string +}; + +type SetLanguagesPayload = { + languageCodes: string[], + activeLanguage?: string +}; + +type SetActiveLanguagePayload = { + languageCode: string +}; + +type LocalizeProps = { + currentLanguage: string, + translate: Translate +}; + +export type InitializeAction = BaseAction<'@@localize/INITIALIZE', InitializePayload>; +export type AddTranslationAction = BaseAction<'@@localize/ADD_TRANSLATION', AddTranslationPayload>; +export type AddTranslationForLanguageAction = BaseAction<'@@localize/ADD_TRANSLATION_FOR_LANGUGE', AddTranslationForLanguagePayload>; +export type SetActiveLanguageAction = BaseAction<'@@localize/SET_ACTIVE_LANGUAGE', SetActiveLanguagePayload>; +export type SetLanguagesAction = BaseAction<'@@localize/SET_LANGUAGES', SetLanguagesPayload>; + +export type Action = BaseAction< + string, + & InitializePayload + & AddTranslationPayload + & AddTranslationForLanguagePayload + & SetActiveLanguagePayload + & SetLanguagesPayload +>; + +export type ActionLanguageCodes = Action & { languageCodes: string[] }; + +export interface LocalizeStateProps { + currentLanguage: string; + translate: Translate; +} + +export function localeReducer(state: LocaleState, action: Action): LocaleState; + +export function initialize(languageCodes: string[], options: Options): InitializeAction; + +export function addTranslation(translation: Object): AddTranslationAction; + +export function addTranslationForLanguage(translation: Object, language: string): AddTranslationForLanguageAction; + +export function setLanguages(languageCodes: string[], activeLanguage: string): SetLanguagesAction; + +export function setActiveLanguage(languageCode: string): SetActiveLanguageAction; + +export function getTranslations(state: LocaleState): Translations; + +export function getLanguages(state: LocaleState): Language[]; + +export function getOptions(state: LocaleState): Options; + +export function getActiveLanguage(state: LocaleState): Language; + +export function getTranslate(state: LocaleState): Selector; + +export function localize(Component: ReactElement, slice?: string): (state: LocaleState) => ComponentClass; \ No newline at end of file diff --git a/src/modules/locale.js b/src/modules/locale.js index 1eb2e64..f435e89 100644 --- a/src/modules/locale.js +++ b/src/modules/locale.js @@ -194,7 +194,7 @@ export const localeReducer = (state: LocaleState = initialState, action: Action) /** * ACTION CREATORS */ -export const initialize = (languageCodes: string[], options: Options = defaultTranslateOptions) => ({ +export const initialize = (languageCodes: string[], options: Options = defaultTranslateOptions): InitializeAction => ({ type: INITIALIZE, payload: { languageCodes, options } });