0.1.3-alpha.1 • Published 9 months ago

@bricco/localization v0.1.3-alpha.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Initialization

  • Create a localization file, ie: /src/locales/sv/common.json|js|ts

  • Create /src/locales/translations.ts

import { initServerTranslations } from '@bricco/localization';

import { Common as CommonSV } from '@/locales/sv/common';

const dictionaries = {
    en: {},
    sv: CommonSV,
  };

const { t, getLang } = initServerTranslations(dictionaries);

export { dictionaries, getLang, t };
  • Add a /localization.d.ts with this code:
import '@bricco/localization';

import { Common as CommonSV } from '@/locales/sv/common';

declare module '@bricco/localization' {
  interface CustomOptions {
    dictionary: typeof CommonSV;
  }
}
  • Edit your main layout.tsx, ie: /src/app/layout.tsx
import { Localization } from '@bricco/localization';
import { dictionaries } from '@/locales/translations';

...
export default async function RootLayout({ children }: { children: React.ReactNode }): Promise<JSX.Element> {
...
  <body ...>
    <Localization dictionaries={dictionaries}>
      {children}
    </Localization>
  </body>
...
}
  • Optional: Add the language switcher to the Admin menu
      <AdminLayout
        menu={menu}
        languages={[
          { id: 'en', label: 'English' },
          { id: 'sv', label: 'Svenska' },
        ]}
        title={'Example'}
        subTitle="@bricco/next-utils"
      >
        {children}
      </AdminLayout>

Usage

Server components

  import { t } from '@/locales/translations';

Client componets

  import { useTranslation } from '@bricco/localization';

  const { t } = useTranslation();

To translate

  // Will fail to compile TS if "String in english" is missing from @/locales/sv/common.
  console.log(t('String in english'))
0.1.3-alpha.1

9 months ago

0.1.3-alpha.0

9 months ago

0.1.3

9 months ago

0.1.2

11 months ago

0.1.1

1 year ago

2.0.1

11 months ago

0.1.0

1 year ago

0.1.0-alpha.0

1 year ago

0.0.18

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago