Getting Started
Install
# using yarn
yarn add tsp-sdk
# using npm
npm install tsp-sdk
Create and Translate
import TSPSDK from 'tsp-sdk';
const translator = new TSPSDK({
name: 'tsp sdk',
isSSR: false
});
translator.create({
env: 'live',
locale: 'en',
translation: {
projectId: 1,
resources: [1],
collections: [2]
}
}).then((res) => {
translator.$t('tsp_test');
translator.$t('tsp_hi', ['tsp']);
translator.$t('tsp_hello', { name: 'tsp' });
translator.$t('tsp_plural', 1, { count: 1 });
translator.$t('tsp_plural', 2, { count: 2 });
translator.$t('tsp_plural1', 1, { count: 1 });
translator.$t('tsp_plural1', 2, { count: 2 });
translator.$t('tsp_plural2', 1, [1]);
translator.$t('tsp_plural2', 2, [2]);
});
More create options, please see Here
Switch language
translator.switchLanguage('zh-Hans').then((translation) => {
translator.$t('tsp_test');
translator.$t('tsp_hi', 'tsp');
translator.$t('tsp_plural1', 1, { count: 1 });
translator.$t('tsp_plural1', 2, { count: 2 });
});
Load more translation
translator.loadTranslation({
resources: [3]
}).then((translation) => {
translator.$t('tsp_r3');
});
loadTranslation supports the second param too. it accepts a language code to specify language
translator.loadTranslation({ resources: [3] }, 'en');
For more details, please see: Here
More Guide
Pluralization
TSP uses CLDR Rules for pluralization, and SDK implements those rules in two forms:
- Use each independent key by adding the __{category} suffix, which category is zero, one, two, few, many, other. Key tsp_plural for example.
- Compose all plural forms into an object, like key tsp_plural1, not recommended
When the SDK can't find the right plural form, it will fall back to using the category one, then other, then use the original key. If all keys can't be found, the SDK will return the key name you pass directly.
For more details, please see: Here
Modularization
If your project has the same key name in 2 resources or collections.
you may want to specify the priority on different pages, to do this, you can simply give a name in each translation set.
then use switchModule
to tell tsp to find translation in this set first.
import TSPSDK from 'tsp-sdk';
const translator = new TSPSDK();
await translator.create({
env: 'live',
locale: 'en',
translation: {
name: 'module1',
resources: [1]
}
})
await translator.loadTranslation({
name: 'module2',
resources: [2]
});
translator.$t('greet');
const res = await translator.switchModule('module2');
translator.$t('greet');
or you can just call the switchModule method
const translator = new TSPSDK();
await translator.create({
env: 'live',
locale: 'en',
translation: {
name: 'module1',
resources: [1]
}
})
translator.$t('greet');
const res = await translator.switchModule({
name: 'module2',
resources: [2]
});
translator.$t('greet');
For more details, please see: Here
the switchLanguage can switch specify translation by given module name as the second param.
translator.switchLanguage('zh-Hans', 'module1');
Merge translation
merge extra translation to the translator in the current locale, if you want to merge to another locale, pass the language code as the second argument.
const data = {
extra_key: 'Extra Data'
};
translator.mergeTranslation(data);
translator.$t('extra_key');
Integrate with Vue
import TSPSDK from 'tsp-sdk';
import Vue from 'vue';
import App from './App.vue';
const forceUpdate = (vm) => {
if (vm.$forceUpdate) {
vm.$nextTick(() => vm.$forceUpdate())
}
if (vm.$children && vm.$children.length) {
for (let i = 0; i < vm.$children.length; i++) {
forceUplate(vm.$children[i]);
}
}
}
const translator = new TSPSDK();
Vue.prototype.$t = translator.$t.bind(translator);
Vue.prototype.$i18n = translator;
const app = new Vue({
render(h) => h(App)
});
await translator.create({
env: 'live',
locale: 'en',
translation: {
name: 'module1',
resources: [1]
},
forceUpdate() {
forceUpdate(app)
}
});
app.$mount('#root');
to be continue
API Reference
https://shopee.git-pages.garena.com/seller-fe/tech/tsp/tsp-sdk/classes/tspsdk.html