-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·146 lines (125 loc) · 5.25 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!doctype html>
<html>
<head>
<title>Knockout.js i18n plugin</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<style>
.content {
padding-top: 1rem;
}
.form-control {
font-family: Consolas, "Courier New";
font-size: .8rem;
background-color: #eee;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
<script src="knockout-i18n.js"></script>
<script type="text/javascript">
function ViewModel() {
var that = this
, rateLimits = { rateLimit: { timeout: 300, method: 'notifyWhenChangesStop' } };
this.bundlesText = ko.observable().extend(rateLimits);
this.bundlesText.subscribe(function(value) {
var bundles;
try {
bundles = JSON.parse(value);
} catch(e) { return; }
ko.i18n.setBundles(bundles);
});
this.bundleName = ko.observable('zh').extend(rateLimits);
this.bundleText = ko.observable().extend(rateLimits);
this.bundleObject = ko.pureComputed(function() {
return {
locale: this.bundleName(),
bundle: this.bundleText()
};
}, this);
this.bundleObject.subscribe(function(value) {
try {
ko.i18n.setBundle(value.locale, JSON.parse(value.bundle));
} catch(e) {}
});
this.subtituteValue = ko.observable('The Value');
// init values
this.bundlesText(JSON.stringify({
en: {
text: 'Plain text without substitutions.',
textWithArgs: 'This sentance has {{{value}}} substituted.',
en_US: 'English',
fr_FR: 'French',
zh_TW: 'Chinese'
},
fr: {
text: 'Texte brut sans substitution.',
textWithArgs: 'Cette phrase a {{{value}}} substitué',
en_US: 'Anglais',
fr_FR: 'Français',
zh_TW: 'Chinois'
}
}, null, 2));
this.bundleText(JSON.stringify({
text: '沒有代入的文字',
textWithArgs: '這句的{{{value}}}是代入值。',
en_US: '英語',
fr_FR: '法語',
zh_TW: '中文'
}, null, 2));
}
function setsLocale(locale) {
return function() {
ko.i18n.locale(locale);
};
}
</script>
</head>
<body>
<nav class="navbar navbar-light bg-faded navbar-fixed-top">
<span class="navbar-text">Internationalization for Knockout.js</span>
</nav>
<div class="container content">
<div class="row">
<div class="col-lg-8">
<div class="form-group">
<label for="txtBundles">ko.i18n.setBundles(bundles);</label>
<textarea id="txtBundles" class="form-control" rows="16" placeholder="bundles" data-bind="textInput: bundlesText"></textarea>
<small class="form-text text-muted">Edit the JSON above and see how the preview changes.</small>
</div>
<div class="form-group">
<label for="txtBundleName">ko.i18n.setBundle(locale, bundle);</label>
<input id="txtBundleName" type="text" class="form-control" placeholder="locale" data-bind="textInput: bundleName" />
</div>
<div class="form-group">
<textarea id="txtBundleText" class="form-control" rows="8" placeholder="bundle" data-bind="textInput: bundleText"></textarea>
<small class="form-text text-muted">Edit values above and see how the preview changes.</small>
</div>
<div class="form-group">
<label for="txtSubstitude">Subtitude Value</label>
<input id="txtSubstitude" class="form-control" data-bind="textInput: subtituteValue"/>
</div>
</div>
<div class="col">
<label> </label>
<div class="card">
<div class="card-header">
Live Preview
</div>
<div class="card-block">
<p data-bind="i18n: 'text'"></p>
<p data-bind="i18n: 'textWithArgs', i18n-options: { value: subtituteValue }"></p>
<a href="javascript:" class="btn btn-outline-primary" data-bind="click: setsLocale('en'), i18n: 'en_US'"></a>
<a href="javascript:" class="btn btn-outline-success" data-bind="click: setsLocale('fr'), i18n: 'fr_FR'"></a>
<a href="javascript:" class="btn btn-outline-info" data-bind="click: setsLocale('zh'), i18n: 'zh_TW'"></a>
</div>
</div>
</div>
</div>
<script>ko.applyBindings(new ViewModel());</script>
</div>
</body>
</html>