Skip to content

Commit 62217f0

Browse files
committed
app: Bump to v2.0.0
1 parent 0ff38db commit 62217f0

File tree

5 files changed

+269
-29
lines changed

5 files changed

+269
-29
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Dynamic I18n
1+
# Dynamic I18n v2.0.0
22
[![npm](https://img.shields.io/npm/dt/@nuxtjs/dynamic-i18n.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/dynamic-i18n)
33
[![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/dynamic-i18n/latest.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/dynamic-i18n)
44

@@ -7,8 +7,7 @@
77
This module automatically import your translate file from a specific provider.
88

99
# RooadMap
10-
- Integrate [https://github.com/nuxt-community/nuxt-i18n](nuxt-i18n)
11-
- Replace `console.log` and `console.err` with nuxtjs logger
10+
- Integrate [https://github.com/nuxt-community/nuxt-i18n](nuxt-i18n) plugin
1211

1312
# Supported providers
1413
* Google sheets, see example of file [here](https://docs.google.com/spreadsheets/d/1dBsD-EsKb1mHvq4P2Zm4DcOPK2szuxqkkvnTsmbkYhc/edit?usp=sharing)
@@ -35,13 +34,17 @@ This module automatically import your translate file from a specific provider.
3534
| languages | **Required** | `[]` | Contain all the locales we want to import. |
3635
| provider-key | **Required** | `''` | The identifier for the source of the data stored. |
3736
| credentials | **Required** | `{}` | Configuration for the provider. |
38-
| title | Optional | `'Translate'` | Title of the document. |
37+
| id | Optional | None | id of the active document. Use gid for google sheet url to get active tab id |
3938
| maxAge | Optional | `1000 * 60 * 60` | Max age of translate files (60 minutes), use 0 to disable it
4039
| fallbackLocale | Optional | `'en'` | Default language if not founded from the store |
4140
| localeNamespaceStore | Optional | `'i18n'` | Default namespace of i18n locale store. see example of store [here](https://github.com/nuxt/nuxt.js/blob/dev/examples/i18n/store/index.js)|
4241
| outputFilePrefix | Optional | `'locale'` | Prefix of the output file like : {{outputFilePrefix}}-{{language}}.json. |
4342
| staticFolder | Optional | `static` | Specify folder in static
4443

44+
## Migration from v1 to v2
45+
46+
Dynamic i18n has been updated to work with the new version of [https://theoephraim.github.io/node-google-spreadsheet/#/](google-spreadsheet) and the `title` option to locate the tab has been replaced by `id` which is the `gid` identifier that you can get from the google sheet url query params `gid`.
47+
4548
## 📑 License
4649

4750
[MIT License](./LICENSE) - Nuxt Community

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const path = require('path')
22
const library = require('./library')
33

44
const defaults = {
5-
title: 'Translate',
65
provider: 'google-sheet',
76
languages: ['en'],
87
fallbackLocale: 'en',
@@ -34,7 +33,7 @@ module.exports = async function nuxtDynamicI18n(_moduleOptions) {
3433
}
3534

3635
// Load translates from provider
37-
importI18n(importOptions)
36+
await importI18n(importOptions)
3837

3938
this.addPlugin({
4039
src: path.resolve(__dirname, 'plugin.js'),

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nuxtjs/dynamic-i18n",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"license": "MIT",
55
"repository": "https://github.com/nuxt/modules",
66
"homepage": "https://github.com/nuxt/modules/tree/master/modules/dynamic-i18n",
@@ -9,7 +9,7 @@
99
},
1010
"main": "index.js",
1111
"dependencies": {
12-
"google-spreadsheet": "^2.0.4",
12+
"google-spreadsheet": "^3.0.10",
1313
"lodash": "^4.17.5",
1414
"vue-i18n": "^7.6.0"
1515
}

providers/google-sheet-provider.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const _ = require('lodash')
22
const fs = require('fs')
33
const path = require('path')
44

5-
const GoogleSpreadsheet = require('google-spreadsheet')
6-
const KEYS = 'keys'
7-
const VARS = 'vars'
5+
const { GoogleSpreadsheet } = require('google-spreadsheet')
6+
const KEYS = 'KEYS'
7+
const VARS = 'VARS'
88

99
function cb(fn, ...args) {
1010
return new Promise((resolve, reject) => {
@@ -40,16 +40,16 @@ async function extract({ rows, path, conf }) {
4040

4141
if (keys.length > 0) {
4242
_.forEach(conf.languages, (language) => {
43-
if (_.has(row, language.toLowerCase())) {
44-
const value = row[language.toLowerCase()].replace(/ +/g, ' ')
43+
if (_.has(row, language.toUpperCase())) {
44+
const value = row[language.toUpperCase()].replace(/ +/g, ' ')
4545

4646
assignValue(i18n, language, keys, variables, value)
4747
}
4848
})
4949
}
5050
})
5151

52-
if (!path) return Promise.resolve(i18n)
52+
if (!path) return i18n
5353

5454
return save(i18n, path, conf)
5555
}
@@ -64,13 +64,13 @@ async function save(i18n, directory, conf) {
6464
const jsonData = JSON.stringify(i18n[language], null, 4)
6565

6666
try {
67-
fs.writeFileSync(filePath, jsonData);
67+
fs.writeFileSync(filePath, jsonData)
6868
} catch (err) {
69-
Promise.reject(err)
69+
return Promise.reject(err)
7070
}
7171
})
7272

73-
return Promise.resolve(i18n)
73+
return i18n
7474
}
7575

7676
function assignValue(i18n, language, keys, variables, value) {
@@ -87,16 +87,16 @@ function assignValue(i18n, language, keys, variables, value) {
8787
}
8888

8989
function parseValue(value, variables) {
90-
if (_.isEmpty(variables)) return value;
90+
if (_.isEmpty(variables)) return value
9191

92-
var parsed = value;
92+
var parsed = value
9393
_.each(variables, function (variable, index) {
9494
if (!_.isEmpty(variable)) {
95-
parsed = parsed.replace(new RegExp('\\[' + (index + 1) + '\\]', 'g'), '{{' + variable + '}}');
95+
parsed = parsed.replace(new RegExp('\\[' + (index + 1) + '\\]', 'g'), '{{' + variable + '}}')
9696
}
97-
});
97+
})
9898

99-
return parsed;
99+
return parsed
100100
}
101101

102102
function getObjectFromTree(object, tree) {
@@ -110,14 +110,16 @@ async function importI18n({ path, conf, log }) {
110110
if (!checkExportConfigurationFile(conf)) return Promise.reject('Bad configuration file')
111111

112112
const doc = new GoogleSpreadsheet(conf['providerKey'])
113-
await cb(doc.useServiceAccountAuth, conf.credentials)
114-
const info = await cb(doc.getInfo)
115113

116-
const worksheetIndex = _.findIndex(info.worksheets, (worksheet) => worksheet.title === conf.title)
114+
await doc.useServiceAccountAuth(conf.credentials)
115+
await doc.loadInfo()
117116

118-
if (worksheetIndex === -1) return Promise.reject(`Unable to find ${conf.title} worksheet`)
117+
// If no gid provided we load the first tab
118+
const worksheet = conf.id ? doc.sheetsById[conf.id] : doc.sheetsByIndex[0]
119119

120-
const rows = await cb(info.worksheets[worksheetIndex].getRows)
120+
if (!worksheet) return Promise.reject(`Unable to find ${conf.id} worksheet tab`)
121+
122+
const rows = await worksheet.getRows()
121123
const i18n = await extract({ rows, path, conf, log })
122124

123125
log(`Extract: ${rows.length} rows`)
@@ -126,5 +128,5 @@ async function importI18n({ path, conf, log }) {
126128
}
127129

128130
module.exports = {
129-
importI18n: importI18n
130-
};
131+
importI18n
132+
}

0 commit comments

Comments
 (0)