sc-localization
v2.0.1
Published
In-memory low bundle size, type support, file-base localization, for server-rendering and client-rendering js library/framework
Downloads
170
Maintainers
Readme
sc-localization
In-memory, low bundle size, file-base localization package for JavaScript framework/library. Supports types, and works for both server and client. This package provides core functionality that you need to add localization to your project, because of this, you can customize your app localization behavior as you desire. Supports both server and client localization.
Definitions
server localization
: Refers to tools that rely on server/node to render pages.client localization
: Refers to tools that rely on browsers to render pages.parts
: Refers to1st depth keys
of translation object, also refers to the1st depth keys
of locale file if you using single file for locale (more info).partition
: Imagine you don't like to have a big file that holds all your translations, and wants to separate it into smaller files and put relative texts near each other like 'common', 'home', 'about', and..., from now those smaller parts calledpartition
(you may have heard of this as, segment, scope, namespace, and..., but the functionality is more important than its name).
Features
- Type support (check this).
- No global wrapper around your entire app.
- Low bundle size.
- Read/Fetch translation files, thus translation files add zero bytes to the JavaScript final bundle size.
- Zero dependency! (although server localization uses
fs
andpath
, but you already have these in node environment, therefore excluded from bundle size). - Customize the localization behavior as you desire (like, remove/keep default locale from url).
[server localization]
: Loads all translation into memory, therefore getting translation texts in your component costs you1
(more info), also no effect on bundle size and no need to send all translation to client.[server localization]
: No need to useawait
keyword when accessing translation texts.[server localization]
: Able to remove[locale]
folder as wrapper in your routes (if your framework need this routes like Next.js app directory).[client localization]
: Uses fetch to get translations, by settingenablePartition
to true, translation files can be downloaded partition by partition as needed, less bytes to transfer therefore more performance in general! more info.
Limitation
This package only reads/fetches translations and gives them to you, for the rest of the functionality you are responsible for implementing it, this gives you a high level of customization but requires more time to implement also adding a feature, for example, if you want to calculate a value and put it into text translation you need to do it by yourself, although for this situation you can do it by reading this page, but this was just an example.
How It Works?
It's depends on whether you want to use server or client localization, for more info Check this link
Supporting Type
If you using typescript, and your IDE supports intelliSense
you can add your localization type, this helps you avoid mis-spelling words and easy access (less copy/paste) to nested translation when you press .
, cool! hah?! (more info).
Important Notes
- For making this package as fast as possible, some errors deliberately not handled! the most common errors happen when somehow the
init
method not initialize correctly, if you encounter with some errors make sure this method called correctly.
Installation
- Using npm:
npm i sc-localization
- Using yarn
yarn add sc-localization
- Using pnpm
pnpm i sc-localization