Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
next-localization
Advanced tools
next-localization <a target="_blank" rel="noopener noreferrer" href="https://github
The minimalistic localization solution for Next.js, powered by Rosetta
and Next.js 10 Internationalized Routing.
getStaticProps
yarn add next-localization
See example
for full example and locale setup.
Your _app.js
.
import { I18nProvider } from 'next-localization';
import { useRouter } from 'next/router';
export default function MyApp({ Component, pageProps }) {
const router = useRouter();
const { lngDict, ...rest } = pageProps;
return (
<I18nProvider lngDict={lngDict} locale={router.locale}>
<Component {...rest} />
</I18nProvider>
);
}
Any functional component.
import { useI18n } from 'next-localization';
import { useRouter } from 'next/router';
import Link from 'next/link';
const HomePage = () => {
const router = useRouter();
const i18n = useI18n();
// or
const i18nPlural = i18n.withPlural();
return (
<>
<h1>
{i18n.t('title')}}, {i18n.t('welcome', { username })}
</h1>
<p>{i18nPlural('products_count', { items: 2 })}</p>
<Link href="/" locale="en">
<a>Change language to (en)</a>
</Link>
</>
);
};
getStaticProps
Checkout the full example.
The same steps works with getServerSideProps
.
Built-in with Next.js 10 Internationalized Routing
Built-in with Next.js 10 Internationalized Routing
We rely on the native platform api Intl
. If you need to support older browsers (e.g IE11) use polyfills.
We provide a small pluralization i18n.withPlural
utility function. It returns the same ì18n
interface but handles number values as pluralization. The implementation uses Intl.PluralRules
.
import { useRouter } from 'next/router';
import { I18nProvider, useI18n } from 'next-localization';
function Root() {
const router = useRouter();
return (
<I18nProvider
lngDict={{
warning: 'WARNING: {{birds}}',
birds: {
other: 'birds',
one: 'bird',
two: 'two birds',
few: 'some birds'
}
}}
locale={router.locale}>
<Child />
</I18nProvider>
);
}
function Child() {
const i18n = useI18n();
const router = useRouter();
const t = i18n.withPlural();
return <p>{t('warning', { birds: 2 })}</p>; // WARNING: two birds
}
Use DateTimeFormat
, NumberFormat
directly or rely on an external library. The integration will look very similiar.
import { useRouter } from 'next/router';
import { I18nProvider } from 'next-localization';
function Root() {
return (
<I18nProvider
lngDict={{
copyright: 'Copyright: {{date}}'
}}
locale={'en'}>
<Child />
</I18nProvider>
);
}
function Child() {
const router = useRouter();
const date = new Intl.DateTimeFormat(router.locale).format(new Date());
return <p>{t('copyright', { date })}</p>; // Copyright: 8/30/2020
}
If you need access to the i18n
outside of react or react hooks, you can create a custom i18n
instance and pass it to the I18nProvider
.
It's the same interface as useI18n
returns.
import { I18nProvider } from 'next-localization';
import { useRouter } from 'next/router';
const i18n = I18n({
en: { hello: 'Hello, world!' }
});
export default function MyApp({ Component, pageProps }) {
const router = useRouter();
const { lngDict, ...rest } = pageProps;
return (
<I18nProvider i18nInstance={i18n} lngDict={lngDict} locale={router.locale}>
<Component {...rest} />
</I18nProvider>
);
}
Don't forget that a locale change will rerender all components under the I18nProvider
provider.
It's safe to create multiple providers with different language dictionaries. This can be useful if you want to split it into different namespaces.
Here you can see an example how to lazy-load a component with a different locale file. Code splitting is ensured by embedding the JSON file via the babel macro json.macro.
Depending on your application next-localization
might not be sufficient to internationalize your application. You still need to consider:
With some effort those points are very easy to solve and you can still base on a very lightweight localization strategy.
FAQs
next-localization <a target="_blank" rel="noopener noreferrer" href="https://github
The npm package next-localization receives a total of 18,103 weekly downloads. As such, next-localization popularity was classified as popular.
We found that next-localization demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.