2.2.2 • Published 3 years ago
@hurtigruten/nellie-microcopies v2.2.2
nellie.microcopy
NPM package for syncing microcopies from Contentful, and some other nice tools for managing microcopies in your project.
NB, todo
Need to write a script removing emtpy export {} at the bottom of the file (something tsc is adding by default, but unfortunately it fails my script, so right now Im removing it manually before publishing)
How to setup and sync
Install the package
npm i @hurtigruten/nellie-microcopies --save-devThen create a
.envfile for your project and add the following variablesCONTENTFUL_SPACE = "add_your_space" CONTENTFUL_ACCESS_TOKEN = "add_your_token"Then create a
sync-microcopies.json the root of your projectIn this file you add the following
const microcopySync = require('@hurtigruten/nellie-microcopies'); const config = { contentfulConfig: { space: process.env.CONTENTFUL_SPACE, accessToken: process.env.CONTENTFUL_ACCESS_TOKEN, environment: 'master', host: 'cdn.contentful.com' }, baseDir: 'src' }; microcopySync(config);
Add this to your scripts in
package.json:sync:microcopies": "node sync-microcopies.jsThen run
npm run sync:microcopiesfrom the cmd and the sync should start
Desired result
You should now have a /microcopies folder in your /src folder
How to use in application
Add this to your_app_file.tsx
import { useTranslate } from '@hurtigruten/nellie-microcopies';
import { some_set } from '@microcopies/index';
const MyApp = () => {
const fancyLocale = 'en-us';
const translate = useTranslate(activities, (x) => x.activities, fancyLocale);
return (
<p>
Check out this awesome translation: {translate((x) => x.popular.description)}
</p>
)
}
Dont want to pass a locale everytime?
Heres what you can do
- Make your own wrapper of the
useTranslatefunction so that the locale is always provided
The useTranslate function defaults to defaultLocale if no locale is provided