Package Exports
- values-to-keys
Readme
values-to-keys
A lightweight utility to replace translation values with their respective keys, useful for i18n workflows.
Installation
npm install values-to-keys
# or
yarn add values-to-keys
# or
pnpm add values-to-keysFeatures
- 🔍 Replaces string values in translation objects with their dot-notation keys
- 🌲 Handles deeply nested objects and arrays
- 🔄 Works with any translation structure
- 📦 Tiny footprint with zero dependencies
- 🚀 Supports ESM and CommonJS
Usage
The library exports a function that replaces all string values in an object with their respective keys in dot notation.
Basic Example
import { replace } from 'values-to-keys';
// or: import replace from 'values-to-keys';
const translations = {
greetings: {
hello: 'Hello World',
welcome: 'Welcome to our app',
},
errors: {
notFound: 'Not found',
},
};
replace(translations);
console.log(translations);
// Output:
// {
// greetings: {
// hello: 'greetings.hello',
// welcome: 'greetings.welcome',
// },
// errors: {
// notFound: 'errors.notFound',
// },
// }With JSON Translation Files
import translationFile from './locales/en.json';
import { replace } from 'values-to-keys';
replace(translationFile);
// Now translationFile has all string values replaced with their keysUse Cases
This library is designed to work with translation-keys-generator and similar i18n workflows where you need to:
- Generate translation keys from values
- Use the keys in your application code
- Maintain a clean separation between keys and values
API
replaceTranslationsWithKeys(item, namespace?)
The main function that replaces string values with their keys.
item: The object containing translation stringsnamespace: (Optional) Used internally for recursion, don't pass this in the initial call
Returns the modified object (same reference).
replace
An alias for replaceTranslationsWithKeys for convenience.
License
MIT © Mohamed Elhadi