Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translation key separator const and improve rekey script #13

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ lib-cov
out
/dist
/lib
/src/*.js
/scripts/*.js
4 changes: 2 additions & 2 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ limitations under the License.

import fs from "fs";
import { isBinaryExpression, isStringLiteral, isTemplateLiteral, Node } from "@babel/types";
import { Translation, Translations } from "../src";
import { KEY_SEPARATOR, Translation, Translations } from "../src";

export const NESTING_KEY = process.env["NESTING_KEY"] || "|";
export const NESTING_KEY = process.env["NESTING_KEY"] || KEY_SEPARATOR;
export const INPUT_FILE = process.env["INPUT_FILE"] || 'src/i18n/strings/en_EN.json';
export const OUTPUT_FILE = process.env["OUTPUT_FILE"] || 'src/i18n/strings/en_EN.json';

Expand Down
3 changes: 3 additions & 0 deletions scripts/rekey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const translation = _.get(sourceTranslations, oldPath);
if (!translation) {
throw new Error(`"${argv._[0]}" key not present in source translations`);
}
if (_.get(sourceTranslations, newPath)) {
throw new Error(`"${argv._[1]}" key already present in source translations`);
}

function updateTranslations(translations: Translations): void {
const value = _.get(translations, oldPath);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

export const KEY_SEPARATOR = "|";

export * from "./utils";
export * from "./types";
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { KEY_SEPARATOR } from "./index";

/**
* Utility type for string dot notation for accessing nested object properties.
* Based on https://stackoverflow.com/a/58436959
Expand Down Expand Up @@ -57,7 +59,7 @@ type Join<K, P, S extends string = "."> = K extends string | number
* @typeParam MaxDepth the maximum depth to recurse to
* @returns a union type representing all dot (Separator) string notation keys which can access a Leaf (of LeafType)
*/
export type TranslationKey<T extends Translations> = Leaves<T, "|", string | { other: string }>;
export type TranslationKey<T extends Translations> = Leaves<T, typeof KEY_SEPARATOR, string | { other: string }, 4>;

export type Translation = string | {
one?: string;
Expand Down