mirror of
https://github.com/cupcakearmy/svelte-i18n.git
synced 2024-11-16 18:10:43 +01:00
Make tests work again. New utility structure
This commit is contained in:
parent
255547914b
commit
1e8597150c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
*.log
|
||||
dist/
|
||||
dist/
|
||||
coverage/
|
1006
package-lock.json
generated
1006
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@ -17,7 +17,34 @@
|
||||
"format": "prettier --loglevel silent --write \"*.js\" \"src/**/*.js\" && eslint --fix \"*.js\" \"src/**/*.js\"",
|
||||
"prepublishOnly": "npm run format && npm run test"
|
||||
},
|
||||
"jest": {
|
||||
"verbose": true,
|
||||
"testURL": "http://localhost/",
|
||||
"transform": {
|
||||
"^.+\\.jsx?$": "babel-jest"
|
||||
},
|
||||
"testRegex": "(/test/.*|\\.(test|spec))\\.js$",
|
||||
"moduleFileExtensions": [
|
||||
"js"
|
||||
],
|
||||
"coveragePathIgnorePatterns": [
|
||||
"/node_modules/",
|
||||
"/test/"
|
||||
],
|
||||
"coverageThreshold": {
|
||||
"global": {
|
||||
"branches": 90,
|
||||
"functions": 95,
|
||||
"lines": 95,
|
||||
"statements": 95
|
||||
}
|
||||
},
|
||||
"collectCoverage": true
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.0.0-beta.56",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"babel-jest": "^23.4.2",
|
||||
"eslint": "^4.19.1",
|
||||
"eslint-config-prettier": "^2.9.0",
|
||||
"eslint-config-standard": "^11.0.0",
|
||||
@ -28,9 +55,14 @@
|
||||
"eslint-plugin-standard": "^3.1.0",
|
||||
"jest": "^22.4.3",
|
||||
"microbundle": "^0.4.4",
|
||||
"prettier": "^1.12.1"
|
||||
"prettier": "^1.12.1",
|
||||
"svelte": "^2.9.10"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"svelte": "^2.9.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/preset-env": "^7.0.0-beta.56",
|
||||
"deepmerge": "^2.1.1",
|
||||
"object-resolve-path": "^1.1.1"
|
||||
}
|
||||
|
75
src/index.js
75
src/index.js
@ -1,56 +1,65 @@
|
||||
import deepmerge from 'deepmerge'
|
||||
import resolvePath from 'object-resolve-path'
|
||||
|
||||
import { capital, title, upper, lower } from './utils'
|
||||
import Formatter from './formatter'
|
||||
|
||||
const resolvePath = a => a
|
||||
|
||||
export function i18n(store, localesList) {
|
||||
const formatter = new Formatter()
|
||||
const locales = deepmerge.all(localesList)
|
||||
let currentLocale: string
|
||||
const locales = {} // deepmerge.all(localesList)
|
||||
let currentLocale
|
||||
|
||||
const plural = (
|
||||
const getLocalizedMessage = (
|
||||
path,
|
||||
counter,
|
||||
interpolations,
|
||||
locale = currentLocale
|
||||
locale = currentLocale,
|
||||
transformers = undefined,
|
||||
) => {
|
||||
let message = resolvePath(locales[locale], path)
|
||||
|
||||
if (!message) return path
|
||||
|
||||
const choice = Math.min(Math.abs(counter), 2)
|
||||
message = message.split('|')[choice]
|
||||
if (transformers) {
|
||||
for (let i = 0, len = transformers.length; i < len; i++) {
|
||||
message = transformers[i](message)
|
||||
}
|
||||
}
|
||||
|
||||
if (!message) return path
|
||||
|
||||
message = formatter.interpolate(message, interpolations).join('')
|
||||
if (interpolations) {
|
||||
message = formatter.interpolate(message, interpolations).join('')
|
||||
}
|
||||
|
||||
return message.trim()
|
||||
}
|
||||
|
||||
const changeLocale = (newLocale) => {
|
||||
currentLocale = newLocale
|
||||
const _ = (
|
||||
function(path, interpolations, locale = currentLocale) {
|
||||
let message = resolvePath(locales[locale], path)
|
||||
|
||||
if (!message) return path
|
||||
|
||||
message = formatter.interpolate(message, interpolations).join('')
|
||||
|
||||
return message
|
||||
}
|
||||
)
|
||||
|
||||
_.plural = plural
|
||||
|
||||
store.set({ locale: newLocale, _ })
|
||||
const utilities = {
|
||||
capital(...args) {
|
||||
return capital(getLocalizedMessage(...args))
|
||||
},
|
||||
title(...args) {
|
||||
return title(getLocalizedMessage(...args))
|
||||
},
|
||||
upper(...args) {
|
||||
return upper(getLocalizedMessage(...args))
|
||||
},
|
||||
lower(...args) {
|
||||
return lower(getLocalizedMessage(...args))
|
||||
},
|
||||
plural(path, counter, interpolations, locale) {
|
||||
return getLocalizedMessage(path, interpolations, locale, [
|
||||
message => message.split('|')[Math.min(Math.abs(counter), 2)],
|
||||
])
|
||||
},
|
||||
}
|
||||
|
||||
store.setLocale = (locale) => store.fire('locale', locale)
|
||||
store.on('locale', changeLocale)
|
||||
store.setLocale = locale => store.fire('locale', locale)
|
||||
store.on('locale', newLocale => {
|
||||
currentLocale = newLocale
|
||||
const _ = getLocalizedMessage
|
||||
|
||||
Object.assign(_, utilities)
|
||||
store.set({ locale: newLocale, _ })
|
||||
})
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
export { capital, title, upper, lower } from './utils'
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { i18n } from '../src/svelte-i18n'
|
||||
import { i18n } from '../src/index'
|
||||
import { Store } from 'svelte/store.umd'
|
||||
import { capital, title, upper, lower, isObject } from '../src/utils'
|
||||
|
||||
@ -16,7 +16,7 @@ const locales = {
|
||||
wow: {
|
||||
much: {
|
||||
deep: {
|
||||
list: [, 'muito profundo']
|
||||
list: ['Muito', 'muito profundo']
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -137,27 +137,31 @@ describe('Localization utilities', () => {
|
||||
store.setLocale('pt-br')
|
||||
const { _ } = store.get()
|
||||
|
||||
expect(capital(_('phrase'))).toBe('Adoro banana')
|
||||
expect(capital('Adoro banana')).toBe('Adoro banana')
|
||||
expect(_.capital('phrase')).toBe('Adoro banana')
|
||||
})
|
||||
|
||||
it('should title a translated message', () => {
|
||||
store.setLocale('pt-br')
|
||||
const { _ } = store.get()
|
||||
|
||||
expect(title(_('phrase'))).toBe('Adoro Banana')
|
||||
expect(title('Adoro Banana')).toBe('Adoro Banana')
|
||||
expect(_.title('phrase')).toBe('Adoro Banana')
|
||||
})
|
||||
|
||||
it('should lowercase a translated message', () => {
|
||||
store.setLocale('pt-br')
|
||||
const { _ } = store.get()
|
||||
|
||||
expect(lower(_('phrase'))).toBe('adoro banana')
|
||||
expect(lower('adoro banana')).toBe('adoro banana')
|
||||
expect(_.lower('phrase')).toBe('adoro banana')
|
||||
})
|
||||
|
||||
it('should uppercase a translated message', () => {
|
||||
store.setLocale('pt-br')
|
||||
const { _ } = store.get()
|
||||
|
||||
expect(upper(_('phrase'))).toBe('ADORO BANANA')
|
||||
expect(upper('ADORO BANANA')).toBe('ADORO BANANA')
|
||||
expect(_.upper('phrase')).toBe('ADORO BANANA')
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user