Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b9f5e4a
ignore .vscode
Dec 25, 2016
eb2e680
Refactoring: No duck typing i.e. .to(Obbject/JSON)[only] methods trav…
Dec 25, 2016
c53ab53
Tests: dropping DB before start; running validation tests after colle…
Dec 25, 2016
68561ee
added Error on no locale arguments in methods, v 0.2.2
Jan 25, 2017
635d5b8
Check locale name argument validity; fix skipping some i18n paths
Jan 25, 2017
b20bc0b
Check locale name argument validity;
Jan 25, 2017
c0407f4
Don't try to localyze missing fileds
Jan 27, 2017
b254c0b
syncing
May 17, 2017
7400418
syncing
May 17, 2017
868c671
works with String, Number, Boolean, Date; options.defaultLocale; bugf…
May 18, 2017
c747a86
travis, appveyor, codeclimate
May 18, 2017
96287f7
travis, appveyor, codeclimate; package.json npm jshint
May 18, 2017
6d9a1ed
ci's
May 18, 2017
5b05d73
ci's: mongodb
May 18, 2017
d3db63c
^devDeps
May 18, 2017
f8a31c0
support for i18n fields with empty nested objects
pballester Jun 7, 2017
cdeda6c
Merge pull request #2 from pballester/master
Jun 7, 2017
a8618df
tests: m, typo
Jun 7, 2017
183fb96
~ better polymorph; + _i18n_paths internal data (rudimentary for now;…
Oct 7, 2017
9b45317
~ m
Oct 7, 2017
fe5b407
~ m
Oct 7, 2017
9e413a0
~ fix for https://github.com/Automattic/mongoose/issues/5695; v0.3.2
Oct 14, 2017
2ef2a33
- work on document array; + badges
Oct 14, 2017
43b04fa
~ deps, cover
Oct 14, 2017
8d8d5ae
~ .gitignore
Oct 14, 2017
c799730
+ .codeclimate.yml
Oct 14, 2017
ff520b4
~ climate
Oct 14, 2017
82c0757
~ climate
Oct 14, 2017
13037e8
~ climate
Oct 14, 2017
210f56f
~ climate, m
Oct 14, 2017
cbb6c78
~ climate, m2
Oct 14, 2017
f3c263f
~ climate, m3
Oct 14, 2017
606a88e
~ climate, m4
Oct 14, 2017
d24bfc6
~ climate, m5
Oct 14, 2017
821d34d
~ climate, m6
Oct 14, 2017
45b04b1
~ climate, m7
Oct 14, 2017
9d184f5
~ climate, m8
Oct 14, 2017
be10f25
~ climate, m
Oct 15, 2017
2ca1e6a
~ climate, m2
Oct 15, 2017
330e38c
Update README.md
Oct 15, 2017
1dc7cf8
~ fix localize on null val field
Oct 16, 2017
8b3c4db
- package.json: max ver, ci
Jan 11, 2018
f720a9b
~ .gitignore
Jul 31, 2018
a77131e
+ serialization options - params that are passed down to the native t…
Jul 31, 2018
39044e1
~ only inherit own path config
Oct 11, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "2"
plugins:
eslint:
enabled: true
duplication:
enabled: true
config:
languages:
- javascript
fixme:
enabled: true
exclude_patterns:
- "**/**.*"
- "**/**"
- "!index.js"
210 changes: 210 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
env:
es6: true
node: true

parserOptions:
sourceType: "module"
ecmaVersion: 6
ecmaFeatures:
jsx: true

# http://eslint.org/docs/rules/
rules:
# Possible Errors
comma-dangle: [2, never]
no-cond-assign: 2
no-console: 0
no-constant-condition: 2
no-control-regex: 2
no-debugger: 2
no-dupe-args: 2
no-dupe-keys: 2
no-duplicate-case: 2
no-empty: 2
no-empty-character-class: 2
no-ex-assign: 2
no-extra-boolean-cast: 2
no-extra-parens: 0
no-extra-semi: 2
no-func-assign: 2
no-inner-declarations: [2, functions]
no-invalid-regexp: 2
no-irregular-whitespace: 2
no-negated-in-lhs: 2
no-obj-calls: 2
no-regex-spaces: 2
no-sparse-arrays: 2
no-unexpected-multiline: 2
no-unreachable: 2
use-isnan: 2
valid-jsdoc: 0
valid-typeof: 2

# Best Practices
accessor-pairs: 2
block-scoped-var: 0
complexity: ["error", 20]
consistent-return: 0
curly: 0
default-case: 0
dot-location: 0
dot-notation: 0
eqeqeq: 2
guard-for-in: 2
no-alert: 2
no-caller: 2
no-case-declarations: 2
no-div-regex: 2
no-else-return: 0
no-labels: [ "error", { "allowLoop": true } ]
no-empty-pattern: 2
no-eq-null: 2
no-eval: 2
no-extend-native: 2
no-extra-bind: 2
no-fallthrough: 2
no-floating-decimal: 0
no-implicit-coercion: 0
no-implied-eval: 2
no-invalid-this: 0
no-iterator: 2
no-lone-blocks: 2
no-loop-func: 2
no-magic-number: 0
no-multi-spaces: 0
no-multi-str: 0
no-native-reassign: 2
no-new-func: 2
no-new-wrappers: 2
no-new: 2
no-octal-escape: 2
no-octal: 2
no-proto: 2
no-redeclare: 2
no-return-assign: 2
no-script-url: 2
no-self-compare: 2
no-sequences: 0
no-throw-literal: 0
no-unused-expressions: 2
no-useless-call: 2
no-useless-concat: 2
no-void: 2
no-warning-comments: 0
no-with: 2
radix: 2
vars-on-top: 0
wrap-iife: 2
yoda: 0

# Strict
strict: 0

# Variables
init-declarations: 0
no-catch-shadow: 2
no-delete-var: 2
no-label-var: 2
no-shadow-restricted-names: 2
no-shadow: 0
no-undef-init: 2
no-undef: 0
no-undefined: 0
no-unused-vars: 0
no-use-before-define: 0

# Node.js and CommonJS
callback-return: 2
global-require: 2
handle-callback-err: 2
no-mixed-requires: 0
no-new-require: 0
no-path-concat: 2
no-process-exit: 2
no-restricted-modules: 0
no-sync: 0

# Stylistic Issues
array-bracket-spacing: 0
block-spacing: 0
brace-style: 0
camelcase: 0
comma-spacing: 0
comma-style: 0
computed-property-spacing: 0
consistent-this: 0
eol-last: 0
func-names: 0
func-style: 0
id-length: 0
id-match: 0
indent: 0
jsx-quotes: 0
key-spacing: 0
linebreak-style: 0
lines-around-comment: 0
max-depth: 0
max-len: 0
max-nested-callbacks: 0
max-params: 0
max-statements: [2, 45]
new-cap: 0
new-parens: 0
newline-after-var: 0
no-array-constructor: 0
no-bitwise: 0
no-continue: 0
no-inline-comments: 0
no-lonely-if: 0
no-mixed-spaces-and-tabs: 0
no-multiple-empty-lines: 0
no-negated-condition: 0
no-nested-ternary: 0
no-new-object: 0
no-plusplus: 0
no-restricted-syntax: 0
no-spaced-func: 0
no-ternary: 0
no-trailing-spaces: 0
no-underscore-dangle: 0
no-unneeded-ternary: 0
object-curly-spacing: 0
one-var: 0
operator-assignment: 0
operator-linebreak: 0
padded-blocks: 0
quote-props: 0
quotes: 0
require-jsdoc: 0
semi-spacing: 0
semi: 0
sort-vars: 0
keyword-spacing: [ "off", { "after": false, "before": false} ]
space-before-blocks: 0
space-before-function-paren: 0
space-in-parens: 0
space-infix-ops: 0
space-unary-ops: 0
spaced-comment: 0
wrap-regex: 0

# ECMAScript 6
arrow-body-style: 0
arrow-parens: 0
arrow-spacing: 0
constructor-super: 0
generator-star-spacing: 0
no-confusing-arrow: "off"
no-constant-condition": [ "off", { "checkLoops": false } ]
no-class-assign: 0
no-const-assign: 0
no-dupe-class-members: 0
no-this-before-super: 0
no-var: 0
object-shorthand: 0
prefer-arrow-callback: 0
prefer-const: 0
prefer-reflect: 0
prefer-spread: 0
prefer-template: 0
require-yield: 0
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.DS_Store
node_modules/
.idea

.vscode/
coverage/
/jsconfig.json
*.log
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: node_js
node_js:
- 4
- 5
- 6
- 7
- 8
- 9
services:
- mongodb
branches:
only:
- master
# before_install:
# - "npm install -g istanbul"
script:
- "npm run test-ci"
after_script:
- "npm install -g coveralls"
- "cat ./coverage/lcov.info | coveralls"
after_success:
- codeclimate-test-reporter < ./coverage/lcov.info
55 changes: 50 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ mongoose-i18n-localize is a mongoose plugin to support i18n and localization in

It seems like [mongoose-i18n](https://github.com/elrolito/mongoose-i18n) is not longer supported and I didn't get it to work on my machine, so I decided to write my own version.

## Requirements

- Node >= 4.0.0
- Mongoose >= 4.12.2

## Usage

```
Expand All @@ -24,7 +29,8 @@ var schema = new mongoose.Schema({
});

schema.plugin(mongooseI18n, {
locales: ['en', 'de']
locales: ['en', 'de'],
defaultLocale: 'de' // if not specified or invalid - will assume locales[0]
});

var Model = mongoose.model('Name', schema);
Expand All @@ -43,7 +49,9 @@ This will create a structure like:

All validators of `name` get also assigned to `name.en` and `name.de`.

mongoose-i18n-localize adds the methods `toObjectLocalized(resource, locale)` and `toJSONLocalized(resource, locale)` to the i18n schema methods. To set the locale of a resource to `en`, just do:
Currently these field types (or an Array of these) support i18n: `String`, `Number`, `Boolean`, `Date`.

mongoose-i18n-localize adds the methods `toObjectLocalized()` and `toJSONLocalized()` to the i18n schema methods. To set the locale of a resource to `en`, just do:


```js
Expand Down Expand Up @@ -74,9 +82,46 @@ Model.find(function(err, resources) {

Use `toObjectLocalized` or `toJSONLocalized` according to `toObject` or `toJSON`.

If you only want to show only one locale message use the methods
`toObjectLocalizedOnly(resource, locale, localeDefault)` or
`toJSONLocalizedOnly(resource, locale, localeDefault)`.
If you want the fields to assume only the localized values use the methods
`toObjectLocalizedOnly()` or
`toJSONLocalizedOnly()`.


```js
Model.find(function(err, resources) {
var localizedResources = resources.toJSONLocalizedOnly('de');
});

```

`localizedResources` has now the following structure:

```js
[
{
name: 'hallo'
}
]
```

All methods accept 3 optional arguments:
1. ``resource`` (Object) - document(s) to localize
2. ``localeName`` (String) - target locale to populate ``.localized`` subfield(in case of ``.toObjectLocalized(), .toJSONLocalized()``) or the field itself (``.toObjectLocalizedOnly(), .toJSONLocalizedOnly()``). Will use ``options.defaultLocale`` if omitted.
3. ``defaultLocaleName`` (String) - locale to fallback, if the value for ``localeName`` is ``undefined``. Will also use ``options.defaultLocale`` if omitted.
4. ``serialization options`` (object) - params that are passed down to the native ``toObject/toJSON`` schema methods.

```js
Model.find(function(err, resources) {
var localizedResources;
localizedResources = resources.toJSONLocalized();
localizedResources = resources.toJSONLocalizedOnly('de');
localizedResources = resources.toObjectLocalized(resources, 'de', 'en');
localizedResources = resources.toObjectLocalizedOnly('de', 'en');
localizedResources = resources.toObjectLocalized(resources, 'de', 'en', {getters: true});
localizedResources = resources.toObjectLocalizedOnly('de', 'en', {getters: true});
localizedResources = resources.toObjectLocalizedOnly({getters: true});
});
```

# Tests

Expand Down
20 changes: 20 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
environment:
matrix:
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "9"
services:
- mongodb
branches:
only:
- master
install:
- "npm install -g istanbul"
- "npm install"
build: off
test_script:
- "npm run test-ci"
version: "{build}"
Loading