diff --git a/types/vuex-i18n/index.d.ts b/types/vuex-i18n/index.d.ts new file mode 100644 index 0000000000..82f57ff53a --- /dev/null +++ b/types/vuex-i18n/index.d.ts @@ -0,0 +1,82 @@ +// Type definitions for vuex-i18n 1.7 +// Project: https://github.com/dkfbasel/vuex-i18n +// Definitions by: Cedric Kemp +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import _Vue, { PluginObject } from "vue"; + +// augment typings of Vue.js +import "./vue"; + +export interface Translations { + [key: string]: string; +} + +export interface Ii18n { + /** get the current locale */ + locale(): string; + + /** set the current locale (i.e. 'de', 'en') */ + set(locale: string): void; + + /** + * add locale translation to the storage. this will extend existing information + * (i.e. 'de', {'message': 'Eine Nachricht'}) + */ + add(locale: string, translations: Translations): void; + + /** + * replace locale translations in the storage. this will remove all previous + * locale information for the specified locale + */ + replace(locale: string, translations: Translations): void; + + /** + * remove the given locale from the store + */ + remove(locale: string): void; + + /** + * set a fallback locale if translation for current locale does not exist + */ + fallback(locale: string): void; + + /** + * get localized string from store. note that we pass the arguments passed + * to the function directly to the translateInLanguage function + */ + translate(key: string, options: any, pluralization?: number): string | undefined; + + /** + * get localized string from store. note that we pass the arguments passed + * to the function directly to the translateInLanguage function + */ + translate(key: string, defaultValue: string, options: any, pluralization?: number): string | undefined; + + /** + * get localized string from store in a given language if available. + */ + translateIn(locale: string, key: string, options: any, pluralization?: number): string | undefined; + + /** + * get localized string from store in a given language if available. + */ + translateIn(locale: string, key: string, defaultValue: string, options: any, pluralization?: number): string | undefined; + + /** + * check if the given locale translations are present in the store + */ + localeExists(locale: string): boolean; + + /** + * check if the given key is available in the current or fallback locale + */ + keyExists(key: string): boolean; +} + +declare const _default: { + plugin: PluginObject; +}; + +export default _default; diff --git a/types/vuex-i18n/package.json b/types/vuex-i18n/package.json new file mode 100644 index 0000000000..f180048358 --- /dev/null +++ b/types/vuex-i18n/package.json @@ -0,0 +1,7 @@ +{ + "private": true, + "dependencies": { + "vue": ">=2.0.0", + "vuex": ">=2.0.0" + } +} diff --git a/types/vuex-i18n/tsconfig.json b/types/vuex-i18n/tsconfig.json new file mode 100644 index 0000000000..0d820b060e --- /dev/null +++ b/types/vuex-i18n/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es5", + "lib": [ + "es5", + "es2015" + ], + "types": [], + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "noEmit": true, + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "vue.d.ts", + "vuex-i18n-tests.ts" + ] +} \ No newline at end of file diff --git a/types/vuex-i18n/tslint.json b/types/vuex-i18n/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/vuex-i18n/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/vuex-i18n/vue.d.ts b/types/vuex-i18n/vue.d.ts new file mode 100644 index 0000000000..f88cff8dc0 --- /dev/null +++ b/types/vuex-i18n/vue.d.ts @@ -0,0 +1,15 @@ +/** + * Extends interfaces in Vue.js + */ +import Vue from "vue"; +import { Ii18n } from "./index"; + +declare module "vue/types/vue" { + interface Vue { + $i18n: Ii18n; + } + + interface VueConstructor { + i18n: Ii18n; + } +} diff --git a/types/vuex-i18n/vuex-i18n-tests.ts b/types/vuex-i18n/vuex-i18n-tests.ts new file mode 100644 index 0000000000..218bd12ccf --- /dev/null +++ b/types/vuex-i18n/vuex-i18n-tests.ts @@ -0,0 +1,41 @@ +// load vue and vuex instance +import Vue from "vue"; +import { Store } from "vuex"; + +// load vuex i18n module +import vuexI18n from "vuex-i18n"; + +// initialize the vuex store using the vuex module. note that you can change the +// name of the module if you wish +const store = new Store({}); + +// initialize the internationalization plugin on the vue instance. note that +// the store must be passed to the plugin. the plugin will then generate some +// helper functions for components (i.e. this.$i18n.set, this.$t) and on the vue +// instance (i.e. Vue.i18n.set). +Vue.use(vuexI18n.plugin, store); + +// please note that you must specify the name of the vuex module if it is +// different from i18n. i.e. Vue.use(vuexI18n.plugin, store, "myName") + +// add some translations (could also be loaded from a separate file) +// note that it is possible to use placeholders. translations can also be +// structured as object trees and will automatically be flattened by the the +// plugin +const translationsEn = { + content: "This is some {type} content" +}; + +// translations can be kept in separate files for each language +// i.e. resources/i18n/de.json. +const translationsDe = { + "My nice title": "Ein schöner Titel", + content: "Dies ist ein toller Inhalt" +}; + +// add translations directly to the application +Vue.i18n.add("en", translationsEn); +Vue.i18n.add("de", translationsDe); + +// set the start locale to use +Vue.i18n.set("en");