Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intl-plugin #49

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions packages/gasket-intl-plugin/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": true
}
}
]
]
}
6 changes: 6 additions & 0 deletions packages/gasket-intl-plugin/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
"extends": ["godaddy", "plugin:jest/recommended"],
"rules": {
"jest/valid-expect": 0
}
};
1 change: 1 addition & 0 deletions packages/gasket-intl-plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json binary
64 changes: 64 additions & 0 deletions packages/gasket-intl-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.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 (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# 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

# Editor files
.idea
*.iml
.vscode

74 changes: 74 additions & 0 deletions packages/gasket-intl-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.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 (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# 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

# Editor files
.idea
*.iml
.vscode

### ▲ .gitignore contents above
### ▼ additional ignore files for publishing

__mocks__
test/*
.babelrc
.eslintrc.js
.gitattributes
jest.config.js
lib/*.spec.js
34 changes: 34 additions & 0 deletions packages/gasket-intl-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# CHANGELOG

### 3.1.2

- Use url-join package to resolve assetPrefix paths

### 3.1.1

- Allow `@gasket/log-plugin@3`
- Fix security audit failures
- Fix babel configuration

### 3.1.0

- Precache locale files with Workbox

### 3.0.0

- Uses `assetPrefix` from plugin config for CDN support
- Removes use of `apiBase`
- Provides `req.localesDir` for outputDir from plugin config
- Moves locale file endpoint to `/_locales` with better cache control headers

### 2.0.0

- Depend on `@gasket/*` 2.0 packages for Next & Babel 7 support.

### 1.1.0

- Add files from `@gasket/app-template`.

### 1.0.0

- Initial release.
21 changes: 21 additions & 0 deletions packages/gasket-intl-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 GoDaddy Operating Company, LLC.

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.
136 changes: 136 additions & 0 deletions packages/gasket-intl-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# @gasket/intl-plugin

This plugin consolidates all locale files under the build folder.

## Installation

```bash
npm install --save @gasket/intl-plugin
```

## Options

- `blacklistModules` - (string[]) list of modules to exclude from bundling.
- `localesDir` - (string) change the name of directory to gather translation
files from (default: `locales`)
- `outputDir` - (string) path of directory to build locale and manifest files to
(default: `./build/locales`)
- `assetPrefix` - (string) change the default path to `/_locale` endpoint by
adding a path prefix here. (default: ''). Used for setting up CDN support
for locale files. `next.assetPrefix` will be used be unless specified here.
- `languageMap` - (object) specify a language mapping here if required
(default: {}), e.g.
```
{
'zh-HK': 'zh-TW',
'zh-SG': 'zh-CN'
}
```
Using this example, if a customer's language is set to `zh-HK`, then the
application will load localization data for `zh-TW` instead.
- `defaultLanguage` - (string) specify a default language to fall back to if
none of the fallback language translations are available (default: `en-US`)

### Config Example

```js
// gasket.config.js

module.exports = {
intl: {
blacklistModules: ['some-module-with-unwanted-locales'],
}
}
```

## Usage

Add the 'intl-plugin' to gasket config.

## Directory Structure

##### Directly under the app folder

Create a `locales` folder in the application root and add a en-US.json file and
start adding localization keys into it.

##### Under a namespace

Create a `locales` folder in the application root, create a `en-US` *folder*
under `locales` and add files `<namespace>.json` with localization keys.

## After translation

`intl-plugin` provides a [service worker config][./service-worker-plugin/README.md]
that adds next.js static assets to precache. This config expects that you will
translate the contents of the `en-US` folder
into other folders corresponding to the locales of the translations (e.g. `da-DK`).

##### Language Fallback

The service worker should do a sequence of checks to see which translation to
use for a given language.

1. Exact language match, e.g. `da-DK` to match with `da-DK`.
2. Language match, e.g. `da-XX` to match with `da`.
3. Fallback to US language, e.g. `ar-MA` to fall back to `en-US`.

## The intlLanguage hook

When determining what assets to precache, `intl-plugin` defaults to reading the
first language provided in the `accept-language` header. However, you can
override this behavior by adding an `intlLanguage` hook. The `intlLanguage` hook
takes the following parameters:

- `gasket` - (object) The Gasket config
- `language` - (string) Default language specified by Gasket Intl
- `req` - (string) The request

It should then return a string indicating the user's language, or null if this
language cannot be found. `intl-plugin` will populate `intl.language` in the
react state with this value, and use it for future language operations. If null
is returned, Gasket will use `en-US`.

#### Usage example

```js
module.exports = {
hooks: {
intlLanguage: async function intlLanguageHook(gasket, language, req) {
const { env } = gasket.config;
// Always use en-US in dev for some reason....
if(env === 'dev') return 'en-US';
// This example could be handled via languageMap, but...
if(language.includes('fr')) {
return 'fr-FR';
}
// If no special cases apply, use the default language provided by Gasket.
return language;
}
}
}
```


## How it works

When you build your app, the plugin looks for `locale` directories in the app
root and in node modules. Each file is then read and output to the build or
outputDir with a hash added to the filename. This hash is based on the contents
of the locale file, so if there are any changes, the file will get a new hash to
ensure it is unique.

Locale files are served with a long cache expiration time. This allows browsers
to cache the locale files, and only versions with hash name changes will be
newly downloaded.

In order for the app to know the hash name for a particular locale file, a
locale manifest is generated at build time. This file should not be cached by
the browser, nor should it placed on a CDN during deployments. In most cases,
the manifest contents will be served with the first page-render.

To load and utilize locale files on your app, see the [@gasket/intl] package.



[@gasket/intl]: ../gasket-intl/README.md
Loading