Skip to content

Commit

Permalink
Merge pull request #12 from Microsoft/API_1.4_add_locale
Browse files Browse the repository at this point in the history
Api 1 4 add locale
  • Loading branch information
AviSander authored Jan 10, 2017
2 parents dd4d7ba + 0e8de0b commit 388670c
Show file tree
Hide file tree
Showing 11 changed files with 2,572 additions and 6 deletions.
1,282 changes: 1,282 additions & 0 deletions .api/v1.4.0/PowerBI-visuals.d.ts

Large diffs are not rendered by default.

951 changes: 951 additions & 0 deletions .api/v1.4.0/schema.capabilities.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions .api/v1.4.0/schema.dependencies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"PBI_API_VERSION": "v1.4.0",
"type": "object",
"properties": {
"cranPackages": {
"type": "array",
"description": "An array of the Cran packages required for the custom R visual script to operate",
"items": {
"$ref": "#/definitions/cranPackage"
}
}
},
"definitions": {
"cranPackage": {
"type": "object",
"description": "cranPackage - Defines the name and displayName of a required Cran package",
"properties": {
"name": {
"type": "string",
"description": "The name for this Cran package"
},
"displayName": {
"type": "string",
"description": "The name for this Cran package that is shown to the user"
},
"url": {
"type": "string",
"description": "A url for package documentation in Cran website"
}
},
"required": [
"name",
"url"
],
"additionalProperties": false
}
}
}
95 changes: 95 additions & 0 deletions .api/v1.4.0/schema.pbiviz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"PBI_API_VERSION": "v1.4.0",
"type": "object",
"properties": {
"apiVersion": {
"type": "string",
"description": "Version of the IVisual API"
},
"author": {
"type": "object",
"description": "Information about the author of the visual",
"properties": {
"name": {
"type": "string",
"description": "Name of the visual author. This is displayed to users."
},
"email": {
"type": "string",
"description": "E-mail of the visual author. This is displayed to users for support."
}
}
},
"assets": {
"type": "object",
"description": "Assets used by the visual",
"properties": {
"icon": {
"type": "string",
"description": "A 20x20 png icon used to represent the visual"
}
}
},
"externalJS": {
"type": "array",
"description": "An array of relative paths to 3rd party javascript libraries to load",
"items": {
"type": "string"
}
},
"style" : {
"type": "string",
"description": "Relative path to the stylesheet (less) for the visual"
},
"capabilities": {
"type": "string",
"description": "Relative path to the visual capabilities json file"
},
"visual": {
"type": "object",
"description": "Details about this visual",
"properties": {
"description": {
"type": "string",
"description": "What does this visual do?"
},
"name": {
"type": "string",
"description": "Internal visual name"
},
"displayName": {
"type": "string",
"description": "A friendly name"
},
"externals": {
"type": "array",
"description": "External files (such as JavaScript) that you would like to include"
},
"guid": {
"type": "string",
"description": "Unique identifier for the visual"
},
"visualClassName": {
"type": "string",
"description": "Class of your IVisual"
},
"icon": {
"type": "string",
"description": "Icon path"
},
"version": {
"type": "string",
"description": "Visual version"
},
"gitHubUrl": {
"type": "string",
"description": "Url to the github repository for this visual"
},
"supportUrl": {
"type": "string",
"description": "Url to the support page for this visual"
}
}
}
}
}
70 changes: 70 additions & 0 deletions Tutorial/Locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Localizing your Custom Visuals

Visuals can now know PowerBI's locale, so they can display localized information
(read more about [Supported languages and countries/regions for Power BI](https://powerbi.microsoft.com/en-us/documentation/powerbi-supported-languages/)).<br>
The `locale` string is passed on `IVisualHost`.

See [commit - UPDATE THIS!!!](https://github.com/Microsoft/PowerBI-visuals-sampleBarChart/commit/a521bc6b9930f630861dc08e27330030766ae057) for what was added at this step.

## Localizing the tooltips

In the sample we display the current locale in the tooltip.

![Sample BarChart with Locale](./images/LocaleInSampleBarChart.png)

Each of these bar charts was created under different locale (English, Basque and Hindi).

The BarChart contructor now has a `locale` member which is instantiated in the constructor with the host `locale` instance.

```typescript
private locale: string;
...
this.locale = options.host.locale;
```

A `LocalizaionResources` interface was added, which helps in localizing strings. It defines the required string for each locale, and also the 'defaultValue', which will be displayed if the visual wansn't adapted to this locale.<br>
`myResources` is an instance of this interface, which holds the localized strings:

```typescript
module powerbi.extensibility.visual {

export var myResources: Resources = {};
myResources["LanguageKey"] = {
defaultValue: "English(English)",
localization: {
"ar-SA": "العربية (Arabic)",
"bg-BG": "български (Bulgarian)",
...,
"zh-CN": "中国 (Chinese-Simplified)",
"zh-TW": "中國 (Chinese-Tranditional)"
}
};

}
```
Getting a localized string is easy using `getLocalizedString`.
```typescript
/**
* Returns the localized string in the locale transfared using the key that was given to serch the resources
*
* @param {string} locale - the locale in which PowerBI is currently running
* @param {object} key - specify a key for the string you want localized in your visual
*/
export function getLocalizedString(locale: string, key: string): string {
return myResources && key && myResources[key] && (((myResources[key]).localization[locale])|| (myResources[key]).defaultValue);
}
```

The data for the tooltip is than derived from the current `locale`:

```typescript
private getTooltipData(value: any): VisualTooltipDataItem[] {
let language = getLocalizedString(this.locale,"LanguageKey");
return [{
displayName: value.category,
value: value.value.toString(),
color: value.color,
header: language && "displayed language " + language
}];
}
```
Binary file added Tutorial/images/LocaleInSampleBarChart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pbiviz.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"supportUrl": "",
"gitHubUrl": ""
},
"apiVersion": "1.3.0",
"apiVersion": "1.4.0",
"author": {
"name": "",
"email": ""
Expand Down
11 changes: 8 additions & 3 deletions src/barChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ module powerbi.extensibility.visual {
private barDataPoints: BarChartDataPoint[];
private barChartSettings: BarChartSettings;
private tooltipServiceWrapper: ITooltipServiceWrapper;
private locale: string;

static Config = {
xScalePadding: 0.1,
Expand Down Expand Up @@ -159,6 +160,8 @@ module powerbi.extensibility.visual {
let svg = this.svg = d3.select(options.element)
.append('svg')
.classed('barChart', true);

this.locale = options.host.locale;

this.barContainer = svg.append('g')
.classed('barContainer', true);
Expand Down Expand Up @@ -227,7 +230,7 @@ module powerbi.extensibility.visual {
});

this.tooltipServiceWrapper.addTooltip(this.barContainer.selectAll('.bar'),
(tooltipEvent: TooltipEventArgs<number>) => BarChart.getTooltipData(tooltipEvent.data),
(tooltipEvent: TooltipEventArgs<number>) => this.getTooltipData(tooltipEvent.data),
(tooltipEvent: TooltipEventArgs<number>) => null);

let selectionManager = this.selectionManager;
Expand Down Expand Up @@ -320,11 +323,13 @@ module powerbi.extensibility.visual {
//Perform any cleanup tasks here
}

private static getTooltipData(value: any): VisualTooltipDataItem[] {
private getTooltipData(value: any): VisualTooltipDataItem[] {
let language = getLocalizedString(this.locale,"LanguageKey");
return [{
displayName: value.category,
value: value.value.toString(),
color: value.color
color: value.color,
header: language && "displayed language " + language
}];
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/localization/localizationHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module powerbi.extensibility.visual {

export interface Localization {
"ar-SA": string;
"bg-BG": string;
"ca-ES": string;
"cs-CZ": string;
"da-DK": string;
"de-DE": string;
"el-GR": string;
"en-US": string;
"es-ES": string;
"et-EE": string;
"eU-ES": string;
"fi-FI": string;
"fr-FR": string;
"gl-ES": string;
"he-IL": string;
"hi-IN": string;
"hr-HR": string;
"hu-HU": string;
"id-ID": string;
"it-IT": string;
"ja-JP": string;
"kk-KZ": string;
"ko-KR": string;
"it-LT": string;
"lv-LV": string;
"ms-MY": string;
"nb-NO": string;
"nl-NL": string;
"pl-PL": string;
"pt-BR": string;
"pt-PT": string;
"ro-RO": string;
"ru-RU": string;
"sk-SK": string;
"sl-SI": string;
"sr-Cyrl-RS": string;
"sr-Latn-RS": string;
"sv-SE": string;
"th-TH": string;
"tr-TR": string;
"uk-UA": string;
"vi-VN": string;
"zh-CN": string;
"zh-TW": string;
}

export interface LocalizaionResources {
defaultValue: string;
localization: Localization;
}

export interface Resources {
[key: string]: LocalizaionResources;
}

/**
* Returns the localized string in the locale transfared using the key that was given to serch the resources
*
* @param {string} locale - the locale in which PowerBI is currently running
* @param {object} key - specify a key for the string you want localized in your visual
*/
export function getLocalizedString(locale: string, key: string): string {
return myResources && key && myResources[key] && (((myResources[key]).localization[locale])|| (myResources[key]).defaultValue);
}

}
54 changes: 54 additions & 0 deletions src/localization/localizedResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module powerbi.extensibility.visual {

export var myResources: Resources = {};
myResources["LanguageKey"] = {
defaultValue: "English(English)",
localization: {
"ar-SA": "العربية (Arabic)",
"bg-BG": "български (Bulgarian)",
"ca-ES": "català (Catalan)",
"cs-CZ": "čeština (Czech)",
"da-DK": "dansk (Danish)",
"de-DE": "Deutsche (German)",
"el-GR": "ελληνικά (Greek)",
"en-US": "English (English)",
"es-ES": "español service (Spanish)",
"et-EE": "eesti (Estonian)",
"eU-ES": "Euskal (Basque)",
"fi-FI": "suomi (Finnish)",
"fr-FR": "français (French)",
"gl-ES": "galego (Galician)",
"he-IL": "עברית (Hebrew)",
"hi-IN": "हिन्दी (Hindi)",
"hr-HR": "hrvatski (Croatian)",
"hu-HU": "magyar (Hungarian)",
"id-ID": "Bahasa Indonesia (Indonesian)",
"it-IT": "italiano (Italian)",
"ja-JP": "日本の (Japanese)",
"kk-KZ": "Қазақ (Kazakh)",
"ko-KR": "한국의 (Korean)",
"it-LT": "Lietuvos (Lithuanian)",
"lv-LV": "Latvijas (Latvian)",
"ms-MY": "Bahasa Melayu (Malay)",
"nb-NO": "norsk (Norwegian)",
"nl-NL": "Nederlands (Dutch)",
"pl-PL": "polski (Polish)",
"pt-BR": "português (Portuguese)",
"pt-PT": "português (Portuguese)",
"ro-RO": "românesc (Romanian)",
"ru-RU": "русский (Russian)",
"sk-SK": "slovenský (Slovak)",
"sl-SI": "slovenski (Slovenian)",
"sr-Cyrl-RS": "српски (Serbian)",
"sr-Latn-RS": "srpski (Serbian)",
"sv-SE": "svenska (Swedish)",
"th-TH": "ไทย (Thai)",
"tr-TR": "Türk (Turkish)",
"uk-UA": "український (Ukrainian)",
"vi-VN": "tiếng Việt (Vietnamese)",
"zh-CN": "中国 (Chinese-Simplified)",
"zh-TW": "中國 (Chinese-Tranditional)"
}
};

}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
},
"files": [
"typings/index.d.ts",
".api/v1.3.0/PowerBI-visuals.d.ts",
".api/v1.4.0/PowerBI-visuals.d.ts",
"node_modules/d3/d3.js",
"src/tooltipServiceWrapper.ts",
"src/barChart.ts",
"src/objectEnumerationUtility.ts",
"src/tooltipServiceWrapper.ts"
"src/localization/localizationHelper.ts",
"src/localization/localizedResources.ts"
]
}

0 comments on commit 388670c

Please sign in to comment.