forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a312b7b
Showing
13 changed files
with
1,415 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
indent_size = 2 | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
"extends": "standard" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# General | ||
.DS_Store | ||
.AppleDouble | ||
.LSOverride | ||
|
||
# Icon must end with two \r | ||
Icon | ||
|
||
|
||
# Thumbnails | ||
._* | ||
|
||
# Files that might appear in the root of a volume | ||
.DocumentRevisions-V100 | ||
.fseventsd | ||
.Spotlight-V100 | ||
.TemporaryItems | ||
.Trashes | ||
.VolumeIcon.icns | ||
.com.apple.timemachine.donotpresent | ||
|
||
# Directories potentially created on remote AFP share | ||
.AppleDB | ||
.AppleDesktop | ||
Network Trash Folder | ||
Temporary Items | ||
.apdisk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Paul Gascou-Vaillancourt | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# nuxt-i18n | ||
i18n for Nuxt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { resolve } = require('path') | ||
const merge = require('lodash/merge') | ||
const { generateRoutes } = require('./routes') | ||
|
||
module.exports = function (moduleOptions) { | ||
const defaults = {} | ||
const options = merge(defaults, moduleOptions, this.options.i18n) | ||
|
||
const templatesOptions = { | ||
defaultLocale: options.defaultLocale, | ||
locales: JSON.stringify(options.locales), | ||
fallbackLocale: options.fallbackLocale, | ||
messages: JSON.stringify(options.messages) | ||
} | ||
|
||
this.extendRoutes((routes) => { | ||
const newRoutes = generateRoutes({ | ||
baseRoutes: routes, | ||
locales: options.locales, | ||
defaultLocale: options.defaultLocale, | ||
routesOptions: options.routes | ||
}) | ||
routes.splice(0, routes.length) | ||
routes.unshift(...newRoutes) | ||
}) | ||
|
||
// i18n plugin | ||
this.addPlugin({ | ||
src: resolve(__dirname, './templates/i18n.plugin.js'), | ||
fileName: 'i18n.plugin.js', | ||
options: templatesOptions | ||
}) | ||
|
||
// Routing plugin | ||
this.addPlugin({ | ||
src: resolve(__dirname, './templates/i18n.routing.plugin.js'), | ||
fileName: 'i18n.routing.plugin.js' | ||
}) | ||
|
||
// Store module | ||
this.addTemplate({ | ||
src: resolve(__dirname, './templates/i18n.store.js'), | ||
fileName: 'i18n.store.js', | ||
options: templatesOptions | ||
}) | ||
|
||
// Middleware | ||
this.addTemplate({ | ||
src: resolve(__dirname, './templates/i18n.routing.middleware.js'), | ||
fileName: 'i18n.routing.middleware.js', | ||
options: templatesOptions | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { has } = require('lodash') | ||
|
||
/** | ||
* Generate localized route using Nuxt's generated routes and i18n config | ||
* @param {Array} baseRoutes Nuxt's default routes based on pages/ directory | ||
* @param {Array} locales Locales to use for route generation, should be | ||
* used when recursively generating children routes, | ||
* defaults to app's configured LOCALES | ||
* @return {Array} Localized routes to be used in Nuxt config | ||
*/ | ||
const generateRoutes = ({ baseRoutes, locales, defaultLocale, routesOptions }) => { | ||
const newRoutes = [] | ||
baseRoutes.forEach((baseRoute) => { | ||
locales.forEach((locale) => { | ||
const { component } = baseRoute | ||
let { path, name, children } = baseRoute | ||
if (children) { | ||
children = generateRoutes(children, [locale]) | ||
} | ||
const { code } = locale | ||
if (has(routesOptions, `${name}.${code}`)) { | ||
path = routesOptions[name][code] | ||
} | ||
if (code !== defaultLocale) { | ||
// Add leading / if needed (ie. children routes) | ||
if (path.match(/^\//) === null) { | ||
path = `/${path}` | ||
} | ||
// Prefix path with locale code if not default locale | ||
path = `/${code}${path}` | ||
} | ||
const route = { path, component } | ||
if (name) { | ||
name += `-${code}` | ||
route.name = name | ||
} | ||
if (children) { | ||
route.children = children | ||
} | ||
newRoutes.push(route) | ||
}) | ||
}) | ||
return newRoutes | ||
} | ||
|
||
module.exports = { generateRoutes } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Vue from 'vue' | ||
import VueI18n from 'vue-i18n' | ||
|
||
import i18nStore from './i18n.store' | ||
|
||
Vue.use(VueI18n) | ||
|
||
export default ({ app, store, route, isClient, hotReload }) => { | ||
store.registerModule('i18n', i18nStore) | ||
const i18n = new VueI18n({ | ||
<% if (options.fallbackLocale) { %>fallbackLocale: '<%= options.fallbackLocale %>',<% } %> | ||
<% if (options.messages) { %>messages: <%= options.messages %><% } %> | ||
}) | ||
i18n.locale = store.state.i18n.currentLocale | ||
app.i18n = i18n | ||
// Check locale in URL (same as middleware but exclusive to client) | ||
if (isClient) { | ||
const locales = <%= options.locales %> | ||
const defaultLocale = '<%= options.defaultLocale %>' | ||
// Check if middleware called from hot-reloading, ignore | ||
if (hotReload) return | ||
// Get locale from params | ||
let locale = defaultLocale | ||
locales.forEach(l => { | ||
const regexp = new RegExp('^/' + l.code + '/') | ||
if (route.path.match(regexp)) { | ||
locale = l.code | ||
} | ||
}) | ||
if (locales.findIndex(l => l.code === locale) === -1) { | ||
return error({ message: 'Page not found.', statusCode: 404 }) | ||
} | ||
if (locale === store.state.i18n.currentLocale) return | ||
// Set locale | ||
store.dispatch('i18n/setLocale', { locale }) | ||
app.i18n.locale = locale | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import middleware from './middleware' | ||
|
||
middleware['i18n'] = function authMiddleware ({ app, store, route, error, hotReload, isServer }) { | ||
if (!isServer) return | ||
const locales = <%= options.locales %> | ||
const defaultLocale = '<%= options.defaultLocale %>' | ||
// Check if middleware called from hot-reloading, ignore | ||
if (hotReload) return | ||
// Get locale from params | ||
let locale = defaultLocale | ||
locales.forEach(l => { | ||
const regexp = new RegExp('^/' + l.code + '/') | ||
if (route.path.match(regexp)) { | ||
locale = l.code | ||
} | ||
}) | ||
if (locales.findIndex(l => l.code === locale) === -1) { | ||
return error({ message: 'Page not found.', statusCode: 404 }) | ||
} | ||
if (locale === store.state.i18n.currentLocale) return | ||
// Set locale | ||
store.dispatch('i18n/setLocale', { locale }) | ||
app.i18n.locale = locale | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import './i18n.routing.middleware'; | ||
import Vue from 'vue' | ||
|
||
Vue.mixin({ | ||
methods: { | ||
getLocalizedRoute (route, locale) { | ||
if (!route) return | ||
locale = locale || this.$i18n.locale | ||
if (!locale) return | ||
// If route parameters is a string, consider it as the route's name | ||
if (typeof route === 'string') { | ||
route = { name: route } | ||
} | ||
// Build localized route options | ||
const name = route.name + '-' + locale | ||
const baseRoute = Object.assign({}, route, { name }) | ||
// Resolve localized route | ||
const resolved = this.$router.resolve(baseRoute) | ||
let { href } = resolved | ||
// Handle exception for homepage | ||
if (route.name === 'index') { | ||
href += '/' | ||
} | ||
// Cleanup href | ||
href = (href.match(/^\/\/+$/)) ? '/' : href | ||
return href | ||
}, | ||
getRouteBaseName (route) { | ||
route = route || this.$route | ||
if (!route.name) { | ||
return null | ||
} | ||
const locales = this.$store.state.i18n.locales | ||
for (let i = locales.length - 1; i >= 0; i--) { | ||
const regexp = new RegExp('-' + locales[i].code) | ||
if (route.name.match(regexp)) { | ||
return route.name.replace(regexp, '') | ||
} | ||
} | ||
}, | ||
getSwitchLocaleRoute (locale) { | ||
const name = this.getRouteBaseName() | ||
if (!name) { | ||
return '' | ||
} | ||
const baseRoute = Object.assign({}, this.$route, { name }) | ||
return this.getLocalizedRoute(baseRoute, locale) | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export default { | ||
namespaced: true, | ||
|
||
state: () => ({ | ||
locales: <%= options.locales %>, | ||
currentLocale: '<%= options.defaultLocale %>' | ||
}), | ||
|
||
getters: { | ||
currentLocale: state => state.currentLocale | ||
}, | ||
|
||
mutations: { | ||
I18N_SET_LOCALE (state, { locale }) { | ||
state.currentLocale = locale | ||
} | ||
}, | ||
|
||
actions: { | ||
setLocale ({ commit }, { locale }) { | ||
commit('I18N_SET_LOCALE', { locale }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "nuxt-i18n", | ||
"version": "0.0.1", | ||
"description": "i18n for Nuxt", | ||
"main": "lib/module.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"eslint": "eslint --ext .js ./lib" | ||
}, | ||
"eslintIgnore": [ | ||
"lib/templates" | ||
], | ||
"dependencies": { | ||
"lodash": "^4.17.4", | ||
"vue-i18n": "^7.3.2" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^4.10.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.8.0", | ||
"eslint-plugin-node": "^5.2.1", | ||
"eslint-plugin-promise": "^3.6.0", | ||
"eslint-plugin-standard": "^3.0.1" | ||
} | ||
} |
Oops, something went wrong.