Skip to content

Commit 714c48c

Browse files
authored
docs: added some improvements to sdk dev guide (#480)
1 parent b37a678 commit 714c48c

1 file changed

Lines changed: 78 additions & 21 deletions

File tree

docs/plugin_developer_guide.md

Lines changed: 78 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ To keep user experience consistent, developers of IBM Cloud CLI plug-in should a
372372

373373
4. Use "plug-in" instead of "plugin" in all places.
374374

375-
### 2.2. Name and Decription of Plug-in, Namespace and Command
375+
### 2.2. Name and Description of Plug-in, Namespace and Command
376376

377377
**To name the plug-in for a service**:
378378

@@ -384,8 +384,8 @@ To keep user experience consistent, developers of IBM Cloud CLI plug-in should a
384384
- Use lower case words and hyphen
385385
- Names should be at least 2 characters in length
386386
- Follow a “noun-verb” sequence
387-
- For commands that list objects, use the plural of the object name, e.g. `ibmcloud iam api-keys`. If a plural will not work, or it is already described in namespace, use `list`, such as `ibmcloud app list`.
388-
- For commands that retrieve the details of an object, use the object name, e.g. `ibmcloud iam api-key`. If it does not work, use `show`, e.g. `ibmcloud app show`
387+
- For commands that list objects, use the plural of the object name, e.g. `ibmcloud iam api-keys`. If a plural will not work, or it is already described in namespace, use `list`, such as `ibmcloud account list`.
388+
- For commands that retrieve the details of an object, use the object name, e.g. `ibmcloud iam api-key`. If it does not work, use `show`, e.g. `ibmcloud account show`
389389
- Use common verbs such as add, create, bind, update, delete …
390390
- Use `remove` to remove an object from a collection or another object it associated with. Use `delete` to erase an object.
391391

@@ -413,7 +413,7 @@ The following command names are invalid:
413413

414414
- `route map`: Uses a space instead of a hyphen.
415415

416-
- `route\_map`: Uses unallowed characters (`\_`) instead of a hyphen.
416+
- `route\_map`: Uses prohibited characters (`\_`) instead of a hyphen.
417417

418418
- `add-plugin-repo`: Does not follow "noun-verb" word order. The verb "add" should be the last part of the command name.
419419

@@ -576,7 +576,6 @@ When command was successful, the success message should start with "OK" in green
576576
Creating resource group Test under account TestAccount as user@ibm.com...
577577
OK
578578
Resource group Test was created.
579-
Creating application 'my-app'...
580579
```
581580
582581
The following code snippet can help you print the above message:
@@ -711,7 +710,7 @@ ui.Say("upgrading '%s'...", terminal.EntityNameColor(selected))
711710
#### Prompt override
712711
There must be a way to override the prompt from a command line switch to allow the execution of non interactive scripts.
713712
714-
For the configrmation [y/N] prompt use the -f force option.
713+
For the confirmation [y/N] prompt use the -f force option.
715714
716715
717716
### 2.11. Table Output
@@ -1192,7 +1191,7 @@ Here's the workflow:
11921191
11931192
set -e
11941193
1195-
go get github.com/jteeuwen/go-bindata/...
1194+
go install github.com/go-bindata/go-bindata/...@latest
11961195
11971196
echo "Generating i18n resource file ..."
11981197
$GOPATH/bin/go-bindata -pkg resources -o bluemix/resources/i18n\_resources.go bluemix/i18n/resources
@@ -1202,42 +1201,100 @@ Here's the workflow:
12021201
12031202
4. Initialize i18n
12041203
1205-
`T()` must be initialized before use. During i18n initialization in IBM Cloud CLI, user locale is used if it's set in `~/.bluemix/config.json` (plug-in can get user locale via `PluginContext.Locale()`). Otherwise, system locale is auto discovered (see [jibber\_jabber](https://github.com/cloudfoundry/jibber_jabber)) and used. If system locale is not detected or supported, default locale `en\_US` is then used. Next, we initialize the translate function with the locale. Sample code:
1204+
`T()` must be initialized before use. During i18n initialization in IBM Cloud CLI, user locale is used if it's set in `~/.bluemix/config.json` (plug-in can get user locale via `PluginContext.Locale()`). Otherwise, system locale is auto discovered (see [go-locale](https://github.com/Xuanwo/go-locale)) and used. If system locale is not detected or supported, default locale `en\_US` is then used. Next, we initialize the translate function with the locale. Sample code:
1205+
1206+
**detection.go**
1207+
```go
1208+
1209+
package detection
1210+
1211+
import (
1212+
"github.com/Xuanwo/go-locale"
1213+
)
1214+
1215+
//go:generate counterfeiter . Detector
1216+
type Detector interface {
1217+
DetectLocale() string
1218+
DetectLanguage() string
1219+
}
1220+
1221+
type GoLocaleDetector struct{}
1222+
1223+
// DetectLocale will return the current locale as a string.
1224+
// The format of the locale will be the ISO 639 two-letter language code, a DASH, then an ISO 3166 two-letter country code.
1225+
func (d *GoLocaleDetector) DetectLocale() string {
1226+
userLocaleTag, err := locale.Detect()
1227+
if err != nil {
1228+
return ""
1229+
}
1230+
return userLocaleTag.String()
1231+
}
1232+
1233+
// DetectLanguage will return the current locale territory as a string.
1234+
// The format will be the ISO 3166 two-letter country code.
1235+
func (d *GoLocaleDetector) DetectLanguage() string {
1236+
langTag, err := locale.Detect()
1237+
if err != nil {
1238+
return ""
1239+
}
1240+
base, _ := langTag.Base()
1241+
// ISO3 => ISO 31666
1242+
return base.ISO3()
1243+
}
1244+
````
12061245
12071246
```go
12081247
import (
12091248
"fmt"
12101249
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/i18n"
1250+
goi18n "github.com/nicksnyder/go-i18n/v2/i18n"
12111251
)
12121252

1213-
var T i18n.TranslateFunc = Init(core_config.NewCoreConfig(func(e error) {}), new(JibberJabberDetector))
1253+
1254+
var T i18n.TranslateFunc = initWithLocale(DetermineLocale(core_config.NewCoreConfig(func(error) {}), new(GoLocaleDetector)))
1255+
var bundle *goi18n.Bundle
12141256

12151257
func Init(coreConfig core_config.Repository, detector Detector) i18n.TranslateFunc {
1216-
bundle = i18n.Bundle()
1217-
userLocale := coreConfig.Locale()
1218-
if userLocale != "" {
1219-
return initWithLocale(userLocale)
1220-
}
1221-
}
1258+
return initWithLocale(DetermineLocale(config, detector))
1259+
}
12221260

12231261
func initWithLocale(locale string) i18n.TranslateFunc {
1262+
bundle = i18n.Bundle()
12241263
err := loadFromAsset(locale)
12251264
if err != nil {
12261265
panic(err)
12271266
}
1228-
return i18n.MustTfunc(locale, DEFAULT_LOCALE)
1267+
return i18n.MustTfunc(locale)
1268+
}
1269+
1270+
func DetermineLocale(config core_config.Repository, detector Detector) string {
1271+
if config.Locale() != "" {
1272+
return config.Locale()
1273+
}
1274+
1275+
sysLocale := normalize(DetectLocale())
1276+
if isSupported(sysLocale) {
1277+
return sysLocale
1278+
}
1279+
1280+
locale := defaultLocaleForLang(detector.DetectLanguage())
1281+
if locale != "" {
1282+
return locale
1283+
}
1284+
1285+
return DEFAULT_LOCALE
12291286
}
12301287

12311288
// load translation asset for the given locale
12321289
func loadFromAsset(locale string) (err error) {
1233-
assetName := fmt.Sprintf("all.%s.json", locale)
1234-
assetKey := filepath.Join(resourcePath, assetName)
1290+
assetName := "all." + locale + ".json"
1291+
assetKey := filepath.Join(RESOURCE_PATH, assetName)
12351292
bytes, err := resources.Asset(assetKey)
12361293
if err != nil {
1237-
return
1294+
return
12381295
}
1239-
_, err = bundle.ParseMessageFileBytes(bytes, resourceKey)
1240-
return
1296+
_, err = bundle.ParseMessageFileBytes(bytes, assetName)
1297+
return
12411298
}
12421299
```
12431300

0 commit comments

Comments
 (0)