Hi!
I need to support multiple languages in my react (TS)
project.
I need to do that in different components starting with
the TreeGrid.
Everything I found was that I need to use the cldr
libaray, import some files, use loadCldr and set the culture (using setCulture)
and setCurrencyCode (why if I set the culture first???) to get the correct currency.
OK – so my question no 1 is: how to dynamically load
different languages from cldr to use it in loadCldr?
The second problem is that I need to load different
labels for the TreeGrid using L10n.load. Do I really need to create the JSON
data for every language I need to support or is there an easier way so I just
can say – choose local de or es or fr and I get the standard names for every
part of the component?
Alex
|
npm install cldr-data |
|
import { L10n, loadCldr, setCulture, setCurrencyCode } from '@syncfusion/ej2-base'; |
|
import * as cagregorian from "./ca-gregorian.json";
import * as currencies from "./currencies.json";
import * as numbers from "./numbers.json";
import * as timeZoneNames from "./timeZoneNames.json";
loadCldr(cagregorian, currencies, numbers, timeZoneNames); // load json files
|
|
return (<div className='control-pane'>
<div className='control-section'>
<div className='div-button'>
<ButtonComponent cssClass={'e-info'} onClick={this.onclick.bind(this)}>Change Locale</ButtonComponent>
</div>
<TreeGridComponent dataSource={formatData} allowPaging={true} treeColumnIndex={1} childMapping='subtasks' allowPaging='true' pageSettings={this.pageSettings} DataBound={this.DataBound} allowFiltering='true' filterSettings={this.FilterOptions} height='220'>
<ColumnsDirective>
. . .
</ColumnsDirective>
<Inject services={[Page, Filter, Toolbar]}/>
</TreeGridComponent>
</div>
</div>)
<script>
onclick() {
this.cultureChange = true;
var treeGridobj = document.getElementsByClassName("e-treegrid")[0].ej2_instances[0];
treeGridobj.locale='de'; // need to dynamically change the locale for treegrid
L10n.load(localede); // load corresponding culture text
setCulture('de'); // Change the Grid culture
setCurrencyCode('EUR');// Change the currency code
}
</script> |
|
<script>
L10n.load({
'de': {
'pager': {
'currentPageInfo': '{0} von {1} Seiten',
'firstPageTooltip': 'Zur ersten Seite',
'lastPageTooltip': 'Zur letzten Seite',
'nextPageTooltip': 'Zur nächsten Seite',
'nextPagerTooltip': 'Zum nächsten Pager',
'previousPageTooltip': 'Zurück zur letzten Seit',
'previousPagerTooltip': 'Zum vorherigen Pager',
'totalItemsInfo': '({0} Beiträge)' //customize with your own localized text
},
'treegrid': {
"ClearButton": "klar",
'Collapse All': 'Alles einklappen',
"Contains": "Enthält",
. . .
}
}
});
</script> |
Hi!
I have read the documentation, so there is no need to
repeat it. 😉
Regarding question 1:
As I stated, I need to support multiple languages. So
a static support of one language is not enough!
Why do I need to copy the files into my source-folder when using static
imports? I can directly refer to the node_modules folder.
Regarding question 2:
I found a npm package called @syncfustion/ej2-locale.
So there are standard-labels for all components but I need to implement the
same mechanism as in question 1.
Regarding question 3:
You didn’t understand what I mean. There is just one element
that is localized (totelItemsInfo). The problem is: this is grammatically wrong!
You need text for one value and another for 0 or multiple values (i.e. 1 item/0-
or n-itemS)
There is a fourth issue: How do I localize date values in a treegrid just using loadCldr? This did not work for me.
Kind regards
Alex
|
loadCldr( // loding cldr-data
require('cldr-data/supplemental/numberingSystems.json'),
require('cldr-data/main/de/ca-gregorian.json'),
require('cldr-data/main/de/numbers.json'),
require('cldr-data/main/de/currencies.json'),
require('cldr-data/main/de/timeZoneNames.json'));
setCulture('de'); //set the culture
setCurrencyCode('EUR'); //set the Currencycode
L10n.load(require('@syncfusion/ej2-locale/src/de.json')); // load corresponding culture text
render() {
return <div><button onClick={this.buttonClick.bind(this)}>Chinese</button>
<TreeGridComponent dataSource={Data} locale='de' ref={g => this.treegrid = g} treeColumnIndex={1} childMapping='subtasks' editSettings={this.editSettings} allowPaging={true} toolbar={this.toolbarOptions} >
<ColumnsDirective>
<ColumnDirective field='taskID' headerText='ID' width='70' textAlign='Right' isPrimaryKey={true}/>
. . .
</ColumnsDirective>
<Inject services={[Page, Edit, Toolbar]} />
</TreeGridComponent>
</div>
}
buttonClick(args) { // dynamically change while on button click
loadCldr(
require('cldr-data/main/zh/ca-gregorian.json'),
require('cldr-data/main/zh/numbers.json'),
require('cldr-data/main/zh/currencies.json'),
require('cldr-data/main/zh/timeZoneNames.json'));
this.treegrid.locale = 'zh'; // need to change the locale dynamically for grid
L10n.load(require('@syncfusion/ej2-locale/src/zh.json')); // load corresponding culture text
setCulture('zh'); // Change the Grid culture
setCurrencyCode('CNY');// Change the currency code
} |