Merge pull request #21150 from jaeggerr/vuex-i18n

Add types for vuex-i18n
This commit is contained in:
Daniel Rosenwasser
2017-11-10 13:57:39 -08:00
committed by GitHub
6 changed files with 174 additions and 0 deletions
+82
View File
@@ -0,0 +1,82 @@
// Type definitions for vuex-i18n 1.7
// Project: https://github.com/dkfbasel/vuex-i18n
// Definitions by: Cedric Kemp <https://github.com/jaeggerr>
// 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<Ii18n>;
};
export default _default;
+7
View File
@@ -0,0 +1,7 @@
{
"private": true,
"dependencies": {
"vue": ">=2.0.0",
"vuex": ">=2.0.0"
}
}
+26
View File
@@ -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"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}
+15
View File
@@ -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<V extends Vue = Vue> {
i18n: Ii18n;
}
}
+41
View File
@@ -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");