Skip to content

Commit

Permalink
Add format options flag (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedelman authored Jun 27, 2024
1 parent b91818f commit fa842b2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/nx-phrase/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@porscheofficial/nx-phrase",
"version": "0.2.1",
"version": "0.3.0",
"repository": {
"type": "url",
"url": "https://github.com/porscheofficial/nx-plugins",
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-phrase/src/executors/build/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@
"useFallbackLocale": {
"description": "(optional) Pass fallback_locale during locale download to api. All translations that don't exist in the locale currently being downloaded will be complemented with the ones from the fallback_locale",
"type": "boolean"
},
"formatOptions": {
"description": "Options for the various file formats",
"type": "object",
"properties": {
"escapeSingleQuotes": {
"description": "Escape single quotes in L10N strings (optional, default is true). This option is only relevant for fileFormat 'properties'.",
"type": "boolean"
}
}
}
},
"required": ["projectId", "uploadLanguageId"]
Expand Down
10 changes: 10 additions & 0 deletions packages/nx-phrase/src/executors/pull/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
"useFallbackLocale": {
"description": "Pass fallback_locale during locale download to api. All translations that don't exist in the locale currently being downloaded will be complemented with the ones from the fallback_locale",
"type": "boolean"
},
"formatOptions": {
"description": "Options for the various file formats",
"type": "object",
"properties": {
"escapeSingleQuotes": {
"description": "Escape single quotes in L10N strings (optional, default is true). This option is only relevant for fileFormat 'properties'.",
"type": "boolean"
}
}
}
},
"required": ["projectId", "output"]
Expand Down
12 changes: 11 additions & 1 deletion packages/nx-phrase/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from "fs-extra"
import { resolve } from "path"

import { NonSensitiveArgs } from "./types"
import { FormatOptions, NonSensitiveArgs } from "./types"
import { ExecutorContext } from "@nx/devkit"
import { load } from "js-yaml"
import { PhraseClientConfig } from "./phrase"
Expand Down Expand Up @@ -75,6 +75,10 @@ function validateConfig(config: NonSensitiveArgs, projectName: string, requiredC
return !error
}

export const defaultFormatOptions: FormatOptions = {
escapeSingleQuotes: true,
}

export function getConfig(
options: BuildExecutorSchema,
context: ExecutorContext,
Expand Down Expand Up @@ -112,6 +116,12 @@ export function getConfig(
phraseKeyFilter: options.phraseKeyFilter,
useFallbackLocale: options.useFallbackLocale,
useSourceLocaleAsFallback: options.useSourceLocaleAsFallback,
formatOptions: options.formatOptions
? {
escapeSingleQuotes:
options.formatOptions.escapeSingleQuotes ?? defaultFormatOptions.escapeSingleQuotes,
}
: { ...defaultFormatOptions },
} as NonSensitiveArgs

if (!validateConfig(config, projectName, requiredConfigurationProperties)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/nx-phrase/src/lib/phrase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class PhraseClient {

const url = new URL(`${this.#baseUrl}/projects/${projectId}/locales/${id}/download`)
for (const argName in otherArgs) {
if (otherArgs[argName]) {
if (otherArgs[argName] !== undefined) {
url.searchParams.set(argName, otherArgs[argName])
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/nx-phrase/src/lib/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class PullHelper {
async downloadTranslations(locales: PhraseLocale[]): Promise<TranslationsMap> {
const phrase = new PhraseClient(this.config.phraseClientConfig)

const formatOptions = {
"format_options[escape_single_quotes]":
this.config.fileFormat === "properties" ? this.config.formatOptions.escapeSingleQuotes : undefined,
}

const translations: Record<string, string> = {}
for (const locale of locales) {
const { id, name } = locale
Expand All @@ -31,12 +36,13 @@ export class PullHelper {
branch: this.config.branch,
file_format: this.config.fileFormat,
// Use phrase API fallback locale?
...(this.config.useFallbackLocale
...(this.config.useFallbackLocale && locale.fallback_locale
? {
fallback_locale_id: locale.fallback_locale.id,
include_empty_translations: true,
}
: {}),
...formatOptions,
})

translations[name] = localeData
Expand Down
5 changes: 5 additions & 0 deletions packages/nx-phrase/src/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface FormatOptions {
escapeSingleQuotes: boolean
}

export interface NonSensitiveArgs {
projectId: string
branch: string
Expand All @@ -15,4 +19,5 @@ export interface NonSensitiveArgs {
useFallbackLocale: boolean
inputFile: string
workingDirectory: string
formatOptions: FormatOptions
}

0 comments on commit fa842b2

Please sign in to comment.