Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Global I18n Standard
提交
5e200834
G
Global I18n Standard
项目概览
OpenHarmony
/
Global I18n Standard
大约 1 年 前同步成功
通知
0
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Global I18n Standard
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5e200834
编写于
8月 31, 2021
作者:
M
meaty-bag-and-wangwang-meat
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add i18n interfaces
Signed-off-by:
N
meaty-bag-and-wangwang-meat
<
zouzhexi@huawei.com
>
上级
81da9dfe
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
383 addition
and
0 deletion
+383
-0
interfaces/js/kits/include/i18n_addon.h
interfaces/js/kits/include/i18n_addon.h
+54
-0
interfaces/js/kits/src/i18n_addon.cpp
interfaces/js/kits/src/i18n_addon.cpp
+329
-0
未找到文件。
interfaces/js/kits/include/i18n_addon.h
0 → 100644
浏览文件 @
5e200834
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef I18N_ADDON_H
#define I18N_ADDON_H
#include <string>
#include "napi/native_api.h"
#include "napi/native_node_api.h"
#include "locale_config.h"
namespace
OHOS
{
namespace
Global
{
namespace
I18n
{
class
I18nAddon
{
public:
static
napi_value
Init
(
napi_env
env
,
napi_value
exports
);
static
void
Destructor
(
napi_env
env
,
void
*
nativeObject
,
void
*
finalize_hint
);
I18nAddon
();
virtual
~
I18nAddon
();
private:
static
napi_value
GetSystemLanguages
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetSystemCountries
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
IsSuggested
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetDisplayLanguage
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetDisplayRegion
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetSystemLanguage
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetSystemRegion
(
napi_env
env
,
napi_callback_info
info
);
static
napi_value
GetSystemLocale
(
napi_env
env
,
napi_callback_info
info
);
static
void
SetSystemLanguage
(
napi_env
env
,
napi_callback_info
info
);
static
void
SetSystemRegion
(
napi_env
env
,
napi_callback_info
info
);
static
void
SetSystemLocale
(
napi_env
env
,
napi_callback_info
info
);
};
}
// namespace I18n
}
// namespace Global
}
// namespace OHOS
#endif
\ No newline at end of file
interfaces/js/kits/src/i18n_addon.cpp
0 → 100644
浏览文件 @
5e200834
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "i18n_addon.h"
#include <vector>
#include "hilog/log.h"
#include "node_api.h"
namespace
OHOS
{
namespace
Global
{
namespace
I18n
{
#define GET_PARAMS(env, info, num) \
size_t argc = num; \
napi_value argv[num]; \
napi_value thisVar = nullptr; \
void *data = nullptr; \
napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
static
constexpr
OHOS
::
HiviewDFX
::
HiLogLabel
LABEL
=
{
LOG_CORE
,
0xD001E00
,
"I18nJs"
};
using
namespace
OHOS
::
HiviewDFX
;
I18nAddon
::
I18nAddon
()
:
{}
I18nAddon
::~
I18nAddon
()
{
}
void
I18nAddon
::
Destructor
(
napi_env
env
,
void
*
nativeObject
,
void
*
hint
)
{
if
(
nativeObject
==
nullptr
)
{
return
;
}
reinterpret_cast
<
I18nAddon
*>
(
nativeObject
)
->~
I18nAddon
();
}
napi_value
I18nAddon
::
Init
(
napi_env
env
,
napi_value
exports
)
{
napi_status
status
;
napi_property_descriptor
properties
[]
=
{
DECLARE_NAPI_FUNCTION
(
"getSystemLanguages"
,
GetSystemLanguages
),
DECLARE_NAPI_FUNCTION
(
"getSystemCountries"
,
GetSystemCountries
),
DECLARE_NAPI_FUNCTION
(
"isSuggested"
,
IsSuggested
),
DECLARE_NAPI_FUNCTION
(
"getDisplayLanguage"
,
GetDisplayLanguage
),
DECLARE_NAPI_FUNCTION
(
"getDisplayRegion"
,
GetDisplayRegion
),
DECLARE_NAPI_FUNCTION
(
"getSystemLanguage"
,
GetSystemLanguage
),
DECLARE_NAPI_FUNCTION
(
"getSystemRegion"
,
GetSystemRegion
),
DECLARE_NAPI_FUNCTION
(
"getSystemLocale"
,
GetSystemLocale
),
DECLARE_NAPI_FUNCTION
(
"setSystemLanguage"
,
SetSystemLanguage
),
DECLARE_NAPI_FUNCTION
(
"setSystemRegion"
,
SetSystemRegion
),
DECLARE_NAPI_FUNCTION
(
"setSystemLocale"
,
SetSystemLocale
),
};
napi_status
status
=
napi_define_properties
(
env
,
exports
,
sizeof
(
properties
)
/
sizeof
(
napi_property_descriptor
),
properties
);
if
(
status
!=
napi_ok
)
{
HiLog
::
Error
(
LABEL
,
"Failed to set properties at init"
);
return
nullptr
;
}
return
exports
;
}
napi_value
I18nAddon
::
GetSystemLanguages
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
0
);
std
::
vector
<
std
::
string
>
systemLanguages
;
LocaleConfig
::
GetSystemLanguages
(
systemLanguages
);
napi_value
result
;
napi_status
status
=
napi_create_array_with_length
(
env
,
systemLanguages
.
size
(),
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create array"
);
return
nullptr
;
}
for
(
size_t
i
=
0
;
i
<
systemLanguages
.
size
();
i
++
)
{
napi_value
value
;
status
=
napi_create_string_utf8
(
env
,
systemLanguages
[
i
].
c_str
(),
NAPI_AUTO_LENGTH
,
&
value
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
status
=
napi_set_element
(
env
,
result
,
i
,
value
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to set array item"
);
return
nullptr
;
}
}
}
return
result
;
}
napi_value
I18nAddon
::
GetSystemCountries
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
0
);
std
::
vector
<
std
::
string
>
systemCountries
;
LocaleConfig
::
GetSystemCountries
(
systemCountries
);
napi_value
result
;
napi_status
status
=
napi_create_array_with_length
(
env
,
systemCountries
.
size
(),
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create array"
);
return
nullptr
;
}
for
(
size_t
i
=
0
;
i
<
systemCountries
.
size
();
i
++
)
{
napi_value
value
;
status
=
napi_create_string_utf8
(
env
,
systemCountries
[
i
].
c_str
(),
NAPI_AUTO_LENGTH
,
&
value
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
status
=
napi_set_element
(
env
,
result
,
i
,
value
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to set array item"
);
return
nullptr
;
}
}
}
return
result
;
}
napi_value
I18nAddon
::
GetSystemLanguage
(
napi_env
env
,
napi_callback_info
info
)
{
std
::
string
value
=
LocaleConfig
::
GetSystemLanguage
();
napi_value
result
;
napi_status
status
=
napi_create_string_utf8
(
env
,
value
.
c_str
(),
NAPI_AUTO_LENGTH
,
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
return
result
;
}
napi_value
I18nAddon
::
GetSystemRegion
(
napi_env
env
,
napi_callback_info
info
)
{
std
::
string
value
=
LocaleConfig
::
GetSystemRegion
();
napi_value
result
;
napi_status
status
=
napi_create_string_utf8
(
env
,
value
.
c_str
(),
NAPI_AUTO_LENGTH
,
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
return
result
;
}
napi_value
I18nAddon
::
GetSystemLocale
(
napi_env
env
,
napi_callback_info
info
)
{
std
::
string
value
=
LocaleConfig
::
GetSystemLocale
();
napi_value
result
;
napi_status
status
=
napi_create_string_utf8
(
env
,
value
.
c_str
(),
NAPI_AUTO_LENGTH
,
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
return
result
;
}
napi_value
I18nAddon
::
GetDisplayLanguage
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
3
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
localeBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
localeBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
napi_get_value_string_utf8
(
env
,
argv
[
1
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
displayLocaleBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
1
],
displayLocaleBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
bool
sentenceCase
=
true
;
if
(
argv
[
2
]
!=
nullptr
)
{
napi_get_value_bool
(
env
,
argv
[
2
],
&
sentenceCase
);
}
std
::
string
value
=
LocaleConfig
::
GetDisplayLanguage
(
localeBuf
.
data
(),
displayLocaleBuf
.
data
(),
sentenceCase
);
napi_value
result
;
napi_status
status
=
napi_create_string_utf8
(
env
,
value
.
c_str
(),
NAPI_AUTO_LENGTH
,
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
return
result
;
}
napi_value
I18nAddon
::
GetDisplayRegion
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
3
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
localeBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
localeBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
napi_get_value_string_utf8
(
env
,
argv
[
1
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
displayLocaleBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
1
],
displayLocaleBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
bool
sentenceCase
=
true
;
if
(
argv
[
2
]
!=
nullptr
)
{
napi_get_value_bool
(
env
,
argv
[
2
],
&
sentenceCase
);
}
std
::
string
value
=
LocaleConfig
::
GetDisplayRegion
(
localeBuf
.
data
(),
displayLocaleBuf
.
data
(),
sentenceCase
);
napi_value
result
;
napi_status
status
=
napi_create_string_utf8
(
env
,
value
.
c_str
(),
NAPI_AUTO_LENGTH
,
&
result
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to create string item"
);
return
nullptr
;
}
return
result
;
}
napi_value
I18nAddon
::
IsSuggested
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
2
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
languageBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
languageBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
bool
isSuggested
=
false
;
if
(
argv
[
1
]
!=
nullptr
)
{
napi_get_value_string_utf8
(
env
,
argv
[
1
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
regionBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
1
],
regionBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
isSuggested
=
LocaleConfig
::
IsSuggested
(
languageBuf
.
data
(),
regionBuf
.
data
());
}
else
{
isSuggested
=
LocaleConfig
::
IsSuggested
(
languageBuf
.
data
());
}
napi_value
result
;
status
=
napi_get_boolean
(
env
,
isSuggested
,
&
result
);
if
(
status
!=
napi_ok
)
{
HiLog
::
Error
(
LABEL
,
"Create case first boolean value failed"
);
return
nullptr
;
}
return
result
;
}
void
I18nAddon
::
SetSystemLanguage
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
1
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
languageBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
languageBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
LocaleConfig
::
SetSystemLanguage
(
languageBuf
.
data
());
}
void
I18nAddon
::
SetSystemRegion
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
1
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
regionBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
regionBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
LocaleConfig
::
SetSystemLanguage
(
regionBuf
.
data
());
}
void
I18nAddon
::
SetSystemLocale
(
napi_env
env
,
napi_callback_info
info
)
{
GET_PARAMS
(
env
,
info
,
1
);
size_t
len
;
napi_get_value_string_utf8
(
env
,
argv
[
0
],
nullptr
,
0
,
&
len
);
std
::
vector
<
char
>
localeBuf
(
len
+
1
);
status
=
napi_get_value_string_utf8
(
env
,
argv
[
0
],
localeBuf
.
data
(),
len
+
1
,
&
len
);
if
(
status
!=
napi_ok
)
{
context
.
SetErrorMsg
(
"Failed to get string item"
);
return
nullptr
;
}
LocaleConfig
::
SetSystemLanguage
(
localeBuf
.
data
());
}
napi_value
Init
(
napi_env
env
,
napi_value
exports
)
{
return
I18nAddon
::
Init
(
env
,
exports
);
}
static
napi_module
g_i18nModule
=
{
.
nm_version
=
1
,
.
nm_flags
=
0
,
.
nm_filename
=
nullptr
,
.
nm_register_func
=
Init
,
.
nm_modname
=
"i18n"
,
.
nm_priv
=
((
void
*
)
0
),
.
reserved
=
{
0
}
};
extern
"C"
__attribute__
((
constructor
))
void
AbilityRegister
()
{
napi_module_register
(
&
g_i18nModule
);
}
}
// namespace I18n
}
// namespace Global
}
// namespace OHOS
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录