gpt4 book ai didi

Resources for I18N in C++ [closed](C++中的I18N资源[已关闭])

转载 作者:bug小助手 更新时间:2023-10-25 14:36:57 37 4
gpt4 key购买 nike




I'm playing around with the locale and I18N stuff in c++ and have been looking for real world examples. I've read through the Josuttis chapter on I18N in his book (2nd Edition for C++11 to be released April 2012), and found it useful but with no real world examples to draw from I've no idea if I'm following best practices are committing beginner mistakes. What resources can StackOverflow point me towards both on the web and in print for doing I18N stuff in C++?

我正在研究c++中的区域设置和i18n内容,并一直在寻找真实世界的示例。我已经通读了他的书(第二版C++11将于2012年4月发布)中关于I18N的Josuttis一章,发现它很有用,但没有现实世界的例子可供借鉴,我不知道我是否在遵循最佳实践正在犯初学者的错误。StackOverflow可以为我在Web和印刷品上提供哪些资源来用C++做I18N工作?



Also what libraries are available for C++ that makes i18n easier? What's not in the standard library that needs to be? At first glance, it seems that UTF8 support doesn't exist in the standard library.

还有,哪些库可用于C++,从而使i18n更容易?标准库中没有需要的内容是什么?乍一看,标准库中似乎不存在对UTF8的支持。



Edit:

编辑:



After doing some more reading, it seems that C and C++ are both Unicode "agnostic". It also seems that for dealing with data encoded in Unicode/UTF8/16/32 one needs to use a third party library. The crux of this is that the standard library itself only thinks about ISO 8859 and related character pages, which change based on what you're locale is set to. That means I probably want to use the ICU library for strings rather than using std::string or even std::wstring.

再读一遍,似乎C和C++都是Unicode的“不可知者”。似乎还需要使用第三方库来处理以Unicode/UTF8/16/32编码的数据。问题的症结在于,标准库本身只考虑ISO 8859和相关的字符页面,这些页面会根据您的区域设置而变化。这意味着我可能希望对字符串使用ICU库,而不是使用std::string,甚至std::wstring。


更多回答
优秀答案推荐

You've been pointed to GNU gettext, which allows you to replace literal strings with localized versions at run time - one aspect of localization (which is what happens after you've done the internationalization, when someone actually uses your internationalized code in a specific locale). You've also been pointed to the Boost (in particular Boost.Locale) libraries; that is usually a good answer for anything related to C++.

您已经被指向GNU getText,它允许您在运行时将文字字符串替换为本地化版本,这是本地化的一个方面(这是在您完成国际化之后,当有人在特定地区实际使用您的国际化代码时发生的事情)。您还提到了Boost(特别是Boost.Locale)库;对于任何与C++相关的东西,这通常都是一个很好的答案。


Another place you might look is the ICU (International Components for Unicode) project. And as a source of data, you might look at the CLDR (Common Locale Data Repository) as a source of information about different locales; the Unicode web site also has lots of information about other aspects of different cultures because it deals with many languages.

您可以查看的另一个地方是ICU(International Components For Unicode)项目。作为数据源,您可以将CLDR(通用区域设置数据存储库)视为有关不同区域设置的信息的来源;Unicode网站还包含大量有关不同文化的其他方面的信息,因为它涉及多种语言。


And as a final resource for now, a rather specialized one, there is the Olson Time Zone database (see also Wikipedia on TZ database — and the released data is available via FTP), which is updated multiple times each year to keep track of the way different countries change their rules on when to change between winter and summer (daylight saving and standard) time.

作为目前的最后一个资源,一个相当专业的资源是Olson时区数据库(另请参阅TZ数据库上的维基百科-发布的数据可通过FTP获得),该数据库每年更新多次,以跟踪不同国家在冬季和夏季(夏令时和标准时间)之间更改规则的方式。



Take a look at wxWidgets examples of internationalization. It has a non-gnu implementation of the translation mechanism used in gettext (mentioned previously). You can use it even in commercial application because the wxWidgets license allows you to do so.

请看国际化的wxWidget示例。它具有在getText中使用的翻译机制的非GNU实现(前面提到过)。您甚至可以在商业应用程序中使用它,因为wxWidget许可证允许您这样做。



If you are just looking at a method for internationalizing the application gettext is the starting point.

如果您只是在寻找一种国际化应用程序的方法,则GetText是起点。



Take a look at GNU gettext

看一看GNU getText



It would be interesting to know which C++ library are you using, as some GUI libraries already provide i18n support.

知道您使用的是哪个C++库会很有趣,因为一些GUI库已经提供了I18N支持。



I second GNU gettext. However, if you're using Qt (and chances are good if you want to do GUI with C++), Qt comes with its own version of gettext and a fine translation tool (Qt Linguist) that makes things easier, and can also be used from non-GUI applications without overhead. We have it even in a service.

I Second GNU GetText。然而,如果你正在使用Qt(如果你想用C++做图形用户界面,机会是很大的),Qt附带了它自己版本的getText和一个很好的翻译工具(Qt语言学家),它使事情变得更容易,而且也可以从非图形用户界面应用程序中使用,而不需要额外的开销。我们甚至在服务中也有它。



However, regarding what C++ lacks, just what comes to my mind:

然而,关于C++缺少什么,我想到的是:




  • Full support for locales with regard to numbers, dates or currency. And remember that locales differ in more than just the decimal separator, including ordering, which brings me to the second point:

  • C++ itself does not have a format function supporting ordered arguments (because in another language, the word order might be different than in your language). You can use boost::format for this. Qt of course supports it as well.



Also have a look at what you had to do for yourself, even if these things are supported. .NET for example comes with a comprehensive list of locales that are ready for use.

也要看看你必须为自己做些什么,即使这些东西是受支持的。例如,.NET附带了一份全面的可供使用的语言环境列表。



Why not keep it simple and roll your own with modern C++:

为什么不保持简单,用现代C++编写您自己的代码:


Internationalization.hpp:

Interationalization.hpp:


#pragma once

#include <map>
#include <string>

enum class supported_language_t
{
english,
chinese
};

inline std::string hello_world_map_key = "hello-world";
inline std::string hello_map_key = "hello";

inline supported_language_t supported_language = supported_language_t::chinese;

std::string get_translated_string(const std::string& key);

Internationalization.cpp:

Internationalization.cpp:


#include "Internationalization.hpp"
#include <stdexcept>
#include <magic_enum.hpp>

std::map<std::string, std::string> english_translations =
{
{hello_world_map_key, "Hello, world!"},
{hello_map_key, "Hello {}!"}
};

std::map<std::string, std::string> chinese_translations =
{
{hello_world_map_key, "你好,世界!"},
{hello_map_key, "你好 {}!"}
};

std::string get_translated_string(const std::string& key)
{
switch (supported_language)
{
case supported_language_t::chinese:
return chinese_translations.at(key);

case supported_language_t::english:
return english_translations.at(key);

default:
throw std::runtime_error("Unsupported language: " + std::string(magic_enum::enum_name(supported_language)));
}
}

How to use it:

使用方法:


const auto hello_world_transated = get_translated_string(hello_world_map_key); // "你好,世界!"
const auto hello_translated = std::format(get_translated_string(hello_map_key), "StackOverflow"); // "你好 StackOverflow!"

As you can see it works even with placeholders thanks to C++20's std::format.

正如你所看到的,它甚至可以使用占位符,这要归功于C++20‘S std::格式。



For the UTF-8 support, take a look at UTF-8 CPP library.

有关UTF-8支持,请查看UTF-8CPP库。



If you would like a solution as easy as possible, and you only need to translate your text in different languages, you can take a look at this tool that I am currently working on: cpp-i18n

如果您想要一个尽可能简单的解决方案,并且只需要将您的文本翻译成不同的语言,那么您可以看看我目前正在开发的这个工具:cpp-i18n


It is still in early state, but It is working for simple usages. The goal was to make it as simple as possible to use. A simple roadmap is in the work for the future of this project :)

它仍然处于早期状态,但它正在为简单的用途工作。这样做的目的是让它尽可能简单易用。该项目未来的工作有一个简单的路线图:)



You do not need libraries to accomplish this task. See the MS guide for Satellite DLLs here.

您不需要库来完成此任务。请参阅MS指南卫星DLL在这里。



If you specify to use Unicode in the project settings and use Unicode String functions (instead of char) then the standard use of Satellite DLLs to hold locale specific data (dialog boxes, Strings etc) will be enough.

如果您指定在项目设置中使用Unicode并使用Unicode字符串函数(而不是char),那么标准使用附属DLL来保存特定于区域设置的数据(对话框、字符串等)就足够了。



Windows itself will take care of finding the correct resource so long as the DLL is correctly named. The suffixs needed for the satellite DLLs are here .

只要DLL命名正确,Windows本身就会负责查找正确的资源。附属DLL所需的后缀在此。


更多回答

Qt is not really c++, since it uses MOC, a meta object compiler, but OK. Have a look at copperspice a real portage to C++ of Qt.

Qt并不是真正的C++,因为它使用的是MOC,一个元对象编译器,但OK。让我们来看看Coperspice,这是Qt向C++的真正移植。

Why not? Because Internationalization is not simple.

为什么不行?因为国际化并不简单。

@Lothar: This is approximately the way Java or similar languages do it with ResourceBundles so that approach should be reasonable

@Lothar:这大致就是Java或类似语言使用ResourceBundles的方式,所以这种方法应该是合理的

Well this was designed 30 years ago. Even before we had unicode and programs were maybe a few tens of thousands of lines long. We have come far. Educate yourself and for example just watch Apples WWDC. How can we translate for plurality (gettext can this) or based on he/she grammer (gettext can no). Nothing is esay in modern L17N

这是30年前设计的。甚至在我们有Unicode之前,程序可能有几万行长。我们已经走了很远。教育自己,例如,只需观看苹果公司的WWDC。我们如何才能翻译成复数(gettext这个可以)或基于他/她的语法(gettext不能)。在现代L17N中没有什么是eSAY的

Bit late, but while this isn't necessarily the most flexible method, it looks perfectly fine to me - I don't think most programs need to deal with this (I don't know much about different languages, but in a lot of programs' "moving parts" - bits of strings that are changed - the gender is not changed), and this is an approach I've seen used in a lot of software.

虽然这不一定是最灵活的方法,但对我来说,它看起来非常好--我认为大多数程序不需要处理这个问题(我对不同的语言了解不多,但在许多程序的“移动部分”--字符串的一小部分被改变--性别没有改变),这是我在很多软件中看到的一种方法。

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com