UNPKG

@shopify/theme-language-server-common

Version:

<h1 align="center" style="position: relative;" > <br> <img src="https://github.com/Shopify/theme-check-vscode/blob/main/images/shopify_glyph.png?raw=true" alt="logo" width="141" height="160"> <br> Theme Language Server </h1>

73 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.paramsString = exports.extractParams = exports.translationOptions = exports.toOptions = exports.isPluralizedTranslation = exports.translationValue = exports.renderTranslation = exports.renderKey = exports.PluralizedTranslationKeys = void 0; exports.PluralizedTranslationKeys = ['one', 'few', 'many', 'two', 'zero', 'other']; function renderKey(translation, key) { if (translation[key]) { return `\`${key}:\` ${translation[key]}`; } } exports.renderKey = renderKey; function renderTranslation(translation) { if (typeof translation === 'string') return translation; return [ renderKey(translation, 'zero'), renderKey(translation, 'one'), renderKey(translation, 'two'), renderKey(translation, 'few'), renderKey(translation, 'many'), renderKey(translation, 'other'), ] .filter(Boolean) .join('\n\n---\n\n'); } exports.renderTranslation = renderTranslation; function translationValue(path, translations) { const parts = path.split('.'); let current = translations; for (const key of parts) { if (!current || typeof current === 'string') { return undefined; } current = current[key]; } return current; } exports.translationValue = translationValue; function isPluralizedTranslation(translations) { return Object.keys(translations).every((key) => exports.PluralizedTranslationKeys.includes(key)); } exports.isPluralizedTranslation = isPluralizedTranslation; function toOptions(prefix, translations) { return Object.entries(translations).flatMap(([path, translation]) => { if (typeof translation === 'string' || isPluralizedTranslation(translation)) { return [{ path: prefix.concat(path), translation }]; } else { return toOptions(prefix.concat(path), translation); } }); } exports.toOptions = toOptions; function translationOptions(translations) { return toOptions([], translations); } exports.translationOptions = translationOptions; function extractParams(value) { const regex = /\{\{([^}]+?)\}\}/g; const results = []; let current; while ((current = regex.exec(value)) !== null) { results.push(current[1].trim()); } return results; } exports.extractParams = extractParams; function paramsString(params) { if (params.length === 0) return ''; return `: ` + params.map((param) => `${param}: ${param}`).join(', '); } exports.paramsString = paramsString; //# sourceMappingURL=translations.js.map