Skip to content

Commit

Permalink
Implement localization of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bnason-nf committed Jun 12, 2020
1 parent 0ff981c commit 3f0add4
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

- Implement localization of strings
- Add setting to disable editor context menu items
- Fix result tooltip formatting issue

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ There is a [GitHub issue](https://github.com/microsoft/vscode/issues/14836) requ
be integrated into Visual Studio Code. If that ever happens then this extension will become
obsolete.

## Contributions

If anyone would like to provide translations for the strings in this extension, please contact me
on the [GitHub project page](https://github.com/bnason-nf/findallinfile/issues)

## Legal

Copyright © 2019 Benbuck Nason.
Expand Down
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "findallinfile",
"displayName": "Find All In File",
"description": "Find all occurrences of text in current file",
"displayName": "%displayName%",
"description": "%description%",
"icon": "data/icon.png",
"version": "0.7.0",
"keywords": [
Expand Down Expand Up @@ -49,52 +49,52 @@
"commands": [
{
"command": "findallinfile.findregexnocase",
"title": "Find Regex (Case Insensitive)",
"category": "Find All In File"
"title": "%findregexnocase%",
"category": "%displayName%"
},
{
"command": "findallinfile.findregexnocaseword",
"title": "Find Regex (Case Insensitive, Whole Word)",
"category": "Find All In File"
"title": "%findregexnocaseword%",
"category": "%displayName%"
},
{
"command": "findallinfile.findregexcase",
"title": "Find Regex (Case Sensitive)",
"category": "Find All In File"
"title": "%findregexcase%",
"category": "%displayName%"
},
{
"command": "findallinfile.findregexcaseword",
"title": "Find Regex (Case Sensitive, Whole Word)",
"category": "Find All In File"
"title": "%findregexcaseword%",
"category": "%displayName%"
},
{
"command": "findallinfile.findstringnocase",
"title": "Find String (Case Insensitive)",
"category": "Find All In File"
"title": "%findstringnocase%",
"category": "%displayName%"
},
{
"command": "findallinfile.findstringnocaseword",
"title": "Find String (Case Insensitive, Whole Word)",
"category": "Find All In File"
"title": "%findstringnocaseword%",
"category": "%displayName%"
},
{
"command": "findallinfile.findstringcase",
"title": "Find String (Case Sensitive)",
"category": "Find All In File"
"title": "%findstringcase%",
"category": "%displayName%"
},
{
"command": "findallinfile.findstringcaseword",
"title": "Find String (Case Sensitive, Whole Word)",
"category": "Find All In File"
"title": "%findstringcaseword%",
"category": "%displayName%"
}
],
"configuration": {
"title": "Find All In File",
"title": "%displayName%",
"properties": {
"findAllInFile.editorContextMenu": {
"type": "boolean",
"default": true,
"description": "Show items in editor context menu."
"description": "%editorContextMenu%"
}
}
},
Expand Down Expand Up @@ -146,7 +146,7 @@
"activitybar": [
{
"id": "findallcontainer",
"title": "Find All In File",
"title": "%displayName%",
"icon": "data/icon.svg"
}
]
Expand Down
27 changes: 27 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"copied_to_clipboard": "Copied results to clipboard",
"description": "Find all occurrences of text in current file",
"displayName": "Find All In File",
"editorContextMenu": "Show items in editor context menu.",
"enter_search_regex": "Please enter regular expression to search for",
"enter_search_string": "Please enter string to search for",
"error_no_document": "No active editor document",
"error_regex": "Regex failure: {0}",
"find_error": "ERROR: {0}",
"find_result_string": "{0}\t{1}:{2}-{3}\t{4}",
"findregexcase": "Find Regex (Case Sensitive)",
"findregexcaseword": "Find Regex (Case Sensitive, Whole Word)",
"findregexnocase": "Find Regex (Case Insensitive)",
"findregexnocaseword": "Find Regex (Case Insensitive, Whole Word)",
"findstringcase": "Find String (Case Sensitive)",
"findstringcaseword": "Find String (Case Sensitive, Whole Word)",
"findstringnocase": "Find String (Case Insensitive)",
"findstringnocaseword": "Find String (Case Insensitive, Whole Word)",
"regex_case": "case sensitive regex",
"regex_no_case": "case insensitive regex",
"search_footer": "Found {0} occurrences",
"search_header": "Searching for {0}{1}\"{2}\" in \"{3}\":",
"string_case": "case sensitive string",
"string_no_case": "case insensitive string",
"whole_word": "word"
}
22 changes: 12 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as vscode from "vscode";

import * as findAllInFile from "./findAllInFile";
import { localize } from "./localize";
import { TreeDataProvider, TreeElement } from "./treeDataProvider";

function getActiveDocument(): vscode.TextDocument | undefined {
Expand Down Expand Up @@ -32,7 +33,7 @@ let lastFindString: string = "";

function findRegexCase(): void {
vscode.window.showInputBox({
prompt: "Please enter regular expression to search for",
prompt: localize("enter_search_regex"),
value: lastFindRegex,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -50,7 +51,7 @@ function findRegexCase(): void {

function findRegexCaseWord(): void {
vscode.window.showInputBox({
prompt: "Please enter regular expression to search for",
prompt: localize("enter_search_regex"),
value: lastFindRegex,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -68,7 +69,7 @@ function findRegexCaseWord(): void {

function findRegexNoCase(): void {
vscode.window.showInputBox({
prompt: "Please enter regular expression to search for",
prompt: localize("enter_search_regex"),
value: lastFindRegex,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -86,7 +87,7 @@ function findRegexNoCase(): void {

function findRegexNoCaseWord(): void {
vscode.window.showInputBox({
prompt: "Please enter regular expression to search for",
prompt: localize("enter_search_regex"),
value: lastFindRegex,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -104,7 +105,7 @@ function findRegexNoCaseWord(): void {

function findStringCase(): void {
vscode.window.showInputBox({
prompt: "Please enter string to search for",
prompt: localize("enter_search_string"),
value: lastFindString,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -122,7 +123,7 @@ function findStringCase(): void {

function findStringCaseWord(): void {
vscode.window.showInputBox({
prompt: "Please enter string to search for",
prompt: localize("enter_search_string"),
value: lastFindString,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -140,7 +141,7 @@ function findStringCaseWord(): void {

function findStringNoCase(): void {
vscode.window.showInputBox({
prompt: "Please enter string to search for",
prompt: localize("enter_search_string"),
value: lastFindString,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand All @@ -158,7 +159,7 @@ function findStringNoCase(): void {

function findStringNoCaseWord(): void {
vscode.window.showInputBox({
prompt: "Please enter string to search for",
prompt: localize("enter_search_string"),
value: lastFindString,
}).then((findText: string | undefined) => {
if (findText !== undefined) {
Expand Down Expand Up @@ -191,11 +192,12 @@ function copyResults(provider: TreeDataProvider): void {
let resultString: string = "";
const results: TreeElement[] = provider.getResults();
for (const result of results) {
resultString += `${result.toString()}\n`;
resultString += result.toString();
resultString += "\n";
}

vscode.env.clipboard.writeText(resultString);
vscode.window.showInformationMessage("Copied results to clipboard");
vscode.window.showInformationMessage(localize("copied_to_clipboard"));
}
// Called once on extension init
export function activate(context: vscode.ExtensionContext): void {
Expand Down
4 changes: 3 additions & 1 deletion src/findError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"use strict";

import { localize } from "./localize";

export class FindError {
public readonly text: string;

Expand All @@ -11,6 +13,6 @@ export class FindError {
}

public toString(): string {
return `ERROR: ${this.text}`;
return localize("find_error", this.text);
}
}
11 changes: 10 additions & 1 deletion src/findResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"use strict";

import { localize } from "./localize";

export class FindResult {
public readonly columnBegin: number;
public readonly columnEnd: number;
Expand All @@ -25,6 +27,13 @@ export class FindResult {
}

public toString(): string {
return `${this.index}\t${this.line + 1}:${this.columnBegin + 1}-${this.columnEnd}\t${this.text}`;
return localize(
"find_result_string",
this.index,
this.line + 1,
this.columnBegin + 1,
this.columnEnd,
this.text
);
}
}
77 changes: 77 additions & 0 deletions src/localize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2019 Benbuck Nason

"use strict";

// Originally from https://github.com/shanalikhan/code-settings-sync/blob/master/src/localize.ts

import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";

interface ILanguagePack {
[key: string]: string;
}

function recurseCandidates(rootPath: string, format: string, candidate: string): string {
const filename: string = format.replace("{0}", `.${candidate}`);
const filepath: string = path.resolve(rootPath, filename);
if (fs.existsSync(filepath)) {
return filename;
}
if (candidate.split("-")[0] !== candidate) {
return recurseCandidates(rootPath, format, candidate.split("-")[0]);
}

return format.replace("{0}", "");
}

function resolveLanguagePack(): ILanguagePack {
let options: { locale: string } = { locale: "" };
try {
const config: string = process.env.VSCODE_NLS_CONFIG ?? "{}";
options = {
...options,
...JSON.parse(config)
};
} catch (err) {
throw err;
}

// tslint:disable:no-any
const languageFormat: string = "package.nls{0}.json";
const defaultLanguage: string = languageFormat.replace("{0}", "");
const extension: vscode.Extension<any> | undefined = vscode.extensions.getExtension("bnason-nf.findallinfile");
const rootPath: string = (extension === undefined) ? "" : extension.extensionPath;
const resolvedLanguage: string = recurseCandidates(rootPath, languageFormat, options.locale);
const languageFilePath: string = path.resolve(rootPath, resolvedLanguage);

try {
const defaultLanguageBundle: any = JSON.parse(
(resolvedLanguage !== defaultLanguage)
? fs.readFileSync(path.resolve(rootPath, defaultLanguage), "utf-8")
: "{}"
);

const resolvedLanguageBundle: any = JSON.parse(
fs.readFileSync(languageFilePath, "utf-8")
);

return { ...defaultLanguageBundle, ...resolvedLanguageBundle };
} catch (err) {
throw err;
}
}

const languagePack: ILanguagePack = resolveLanguagePack();

// tslint:disable:no-any no-unsafe-any
export function localize(key: string, ...args: any[]): string {
const localized: string = languagePack[key];

let formatted: string = localized;
for (let index: number = 0; index < args.length; index += 1) {
formatted = formatted.replace(`{${index}}`, `${args[index]}`);
}

return formatted;
}
Loading

0 comments on commit 3f0add4

Please sign in to comment.