Skip to content

feat(new file ext support): properties support #55

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<img src="https://i.hizliresim.com/stgrebn.png" alt="jsontt logo" width="150" />
</p>

# <p align="center"> **🚀 FREE JSON/YAML TRANSLATOR 🆓** </p>
# <p align="center"> **🚀 FREE JSON/YAML/PROPERTIES TRANSLATOR 🆓** </p>

<p align="center">
<a href="https://npmcharts.com/compare/@parvineyvazov/json-translator?minimal=true">
Expand All @@ -22,7 +22,7 @@
</a>
</p>

This package will provide you to translate your JSON/YAML files or JSON objects into different languages FREE.
This package will provide you to translate your JSON/YAML/PROPERTIES files or JSON objects into different languages FREE.

### Types of usages 👀

Expand Down Expand Up @@ -54,6 +54,8 @@ npm i -g @parvineyvazov/json-translator
jsontt <your/path/to/file.json>
or
jsontt <your/path/to/file.yaml/yml>
or
jsontt <your/path/to/file.properties>
```

## How to use it? (video below)
Expand All @@ -62,7 +64,7 @@ jsontt <your/path/to/file.yaml/yml>

## Arguments

- `[path]`: Required JSON/YAML file path `<your/path/to/file.json>`
- `[path]`: Required JSON/YAML/PROPERTIES file path `<your/path/to/file.json>`
- `[path]`: optional proxy list txt file path `<your/path/to/proxy_list.txt>`

## Options
Expand Down Expand Up @@ -108,7 +110,7 @@ jsontt <your/path/to/file.json> --module google --from en --to ar fr zh-CN --nam

### other usage examples

- translate (json/yaml)
- translate (json/yaml/properties)

```bash
jsontt file.json
Expand All @@ -132,7 +134,7 @@ jsontt "C:\folder1\folder\en.json"
jsontt file.json proxy.txt
```

Result will be in the same folder as the original JSON/YAML file.
Result will be in the same folder as the original JSON/YAML/PROPERTIES file.

<br>

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"http-proxy-agent": "^5.0.0",
"inquirer": "^7.0.0",
"loading-cli": "^1.1.0",
"yaml": "^2.3.2"
"yaml": "^2.3.2",
"properties": "^1.2.1"
}
}
6 changes: 4 additions & 2 deletions src/core/core.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as fs from 'fs/promises';
import * as YAML from 'yaml';
import { matchYamlExt } from '../utils/yaml';
import { isPropertiesExt } from '../utils/properties';
import { error, messages } from '../utils/console';
import * as properties from 'properties';

export async function getFile(objectPath: string) {
let json_file: any = undefined;
Expand All @@ -14,7 +16,7 @@ export async function getFile(objectPath: string) {
// and then stringified to JSON string.
json_file = matchYamlExt(objectPath)
? JSON.stringify(YAML.parse(data))
: data;
: (isPropertiesExt(objectPath) ? JSON.stringify(properties.parse(data,null,null)) : data);
})
.catch(_ => {
json_file = undefined;
Expand All @@ -39,7 +41,7 @@ export function getRootFolder(path: string) {
export async function saveFilePublic(path: string, data: any) {
// When path extension is for YAML file, then stringify with YAML encoder.
// Otherwise, default JSON encoder is used.
var json = matchYamlExt(path) ? YAML.stringify(data) : JSON.stringify(data);
var json = matchYamlExt(path) ? YAML.stringify(data) : (isPropertiesExt(path) ? properties.stringify(data,null,null) : JSON.stringify(data));

await fs
.writeFile(path, json, 'utf8')
Expand Down
3 changes: 3 additions & 0 deletions src/core/json_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { objectTranslator } from './json_object';
import { matchYamlExt } from '../utils/yaml';
import { TranslationConfig } from '../modules/modules';
import { getLanguageKeyFromValue } from '../modules/helpers';
import {isPropertiesExt} from "../utils/properties";

export async function fileTranslator(
TranslationConfig: TranslationConfig,
Expand Down Expand Up @@ -68,6 +69,8 @@ export async function getFileFromPath(
}

function getFileExt(latestPath: string): string {
// We also check properties here.
if(isPropertiesExt(latestPath)) return 'properties';
// Check if source file has YAML extension and return the extension ("yml" or "yaml").
const sourceFileMatchYamlExt = matchYamlExt(latestPath);

Expand Down
8 changes: 8 additions & 0 deletions src/properties.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module 'properties' {
export function createStringifier(): any;

export function parse(data: any, options: any, cb: any): any;

export function stringify(stringifier: any, options: any, cb: any): any;
}

7 changes: 7 additions & 0 deletions src/utils/properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
*
* @param objectPath path to file
* @returns File extension without leading "." ("properties" )
*/
export const isPropertiesExt = (objectPath: string) =>
objectPath.endsWith(".properties")
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6053,6 +6053,11 @@ propagate@^2.0.0:
resolved "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==

properties@^1.2.1:
version "1.2.1"
resolved "https://registry.npmmirror.com/properties/-/properties-1.2.1.tgz#0ee97a7fc020b1a2a55b8659eda4aa8d869094bd"
integrity sha512-qYNxyMj1JeW54i/EWEFsM1cVwxJbtgPp8+0Wg9XjNaK6VE/c4oRi6PNu5p7w1mNXEIQIjV5Wwn8v8Gz82/QzdQ==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz"
Expand Down