Skip to content

Commit

Permalink
exclude pathnames
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Nov 14, 2024
1 parent db80062 commit b2c2da1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Place the following script inside your <head> tag
- **data-use-browser-language**: Automatically sets the language based on the user's browser language. Set to `false` to disable.
- **data-exclude-classes**: List CSS class names to exclude from translation, separated by commas (e.g., `chatbot, no-translate`).
- **data-exclude-ids**: List IDs to exclude from translation, separated by commas (e.g., `user-comment, code-snippet` will prevent translation of elements with ID `user-comment` and `code-snippet`).
- **data-exclude-paths**: List URL paths to exclude from translation, separated by commas (e.g., `/admin, /blog` will prevent translation of URLs containing `/admin` and `/blog`). NOTE: Each path should start with a `/`.

#### Server Side Rendering Configuration:
- **data-translation-mode**: Modify the translated pages logic. Set to `subdomain` to point to subdomains (for example: de.domain.com), or `subdirectory` to point to paths (for example: domain.com/de). *DONT USE* if you are not using SSR (SSR only available in higher plans).
Expand Down
9 changes: 9 additions & 0 deletions extractOptionsFromScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function extractOptionsFromScript(window, optsArgs = {
const DATA_USE_BROWSER_LANG = "data-use-browser-language"; // default: true
const DATA_EXCLUDE_CLASSES = "data-exclude-classes";
const DATA_EXCLUDE_IDS = "data-exclude-ids";
const DATA_EXCLUDE_PATHS = "data-exclude-paths";
const DATA_REPLACE_LINKS = "data-replace-links"; // default: true
const DATA_AUTO_CREATE_SELECTOR = "data-auto-create-selector"; // default: true
const DATA_DELAY = "data-timeout"; // default: 0
Expand Down Expand Up @@ -424,6 +425,13 @@ function extractOptionsFromScript(window, optsArgs = {
const mergedExcludeIds = [...excludeIdsByComma, ...excludeIdsBySpace];
const excludeIds = [...new Set(mergedExcludeIds)]; // get unique values

// exclude paths
const excludePathsAttr = (window.translationScriptTag.getAttribute(DATA_EXCLUDE_PATHS) || "").trim()
const excludePathsByComma = excludePathsAttr.split(",").filter(path => !!path).map(path => path.trim());
const excludePathsBySpace = excludePathsAttr.split(" ").filter(path => !!path).map(path => path.trim().replaceAll(",", ""));
const mergedExcludePaths = [...excludePathsByComma, ...excludePathsBySpace];
const excludePaths = [...new Set(mergedExcludePaths)]; // get unique values

// exclude contents
const excludeContentsAttr = (window.translationScriptTag.getAttribute(DATA_EXCLUDE_CONTENTS) || "").trim()
const splittedContents = getTextInsideBrackets(excludeContentsAttr);
Expand Down Expand Up @@ -456,6 +464,7 @@ function extractOptionsFromScript(window, optsArgs = {
createSelector: createSelector,
excludeClasses,
excludeIds,
excludePaths,
excludeContents,
originalLanguage: originalLang,
allowedLanguages: allowedLangs,
Expand Down
1 change: 1 addition & 0 deletions utils/options/setOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function setOptions(window, apiKey, optsArgs) {
pathOptions: optsArgs.pathOptions || {},
apiKey,
excludeIds: optsArgs.excludeIds || [],
excludePaths: optsArgs.excludePaths || [],
excludeClasses: optsArgs.excludeClasses || [],
excludeContents: optsArgs.excludeContents || [],
definedLanguages: getDefinedLanguages(optsArgs.originalLanguage, optsArgs.allowedLanguages),
Expand Down
8 changes: 8 additions & 0 deletions utils/translation/isExcluded.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ function isExcludedId(window, id) {
return globalseoOptions.excludeIds.length && globalseoOptions.excludeIds.some(excludeId => excludeId && id.includes(excludeId))
}
exports.isExcludedId = isExcludedId;

function isExcludedPath(window) {
const globalseoOptions = getGlobalseoOptions(window);
const path = window.location.pathname;

return globalseoOptions.excludePaths.length && globalseoOptions.excludePaths.some(excludePath => excludePath && path.includes(excludePath))
}
exports.isExcludedPath = isExcludedPath;
6 changes: 6 additions & 0 deletions utils/translation/startTranslationCycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { getLanguageFromLocalStorage } = require("../languages/getSelectedLanguag
const modifyHtmlStrings = require("./modifyHtmlStrings");
const { debounce } = require("../debounce");
const { renderSelectorState } = require("../selector/renderSelectorState");
const { isExcludedPath } = require("./isExcluded");

async function startTranslationCycle(window, node, apiKey, delay, shouldOptimizeSEO = false) {
if (window.preventInitialTranslation) {
Expand All @@ -11,6 +12,11 @@ async function startTranslationCycle(window, node, apiKey, delay, shouldOptimize
};
const options = getGlobalseoOptions(window);

if (isExcludedPath(window)) {
renderSelectorState(window, { shouldUpdateActiveLang: true, delay: 0, shouldLog: false })
return window;
}

if (!window.isWorker && options.translationMode == "subdomain") {
await renderSelectorState(window, { shouldUpdateActiveLang: true, delay: 0, shouldLog: false })

Expand Down

0 comments on commit b2c2da1

Please sign in to comment.