mcthesw / game-save-manager

一个简单的游戏存档管理器
https://help.sworld.club
GNU Affero General Public License v3.0
573 stars 20 forks source link

Add i18n support #63

Closed AsterNighT closed 8 months ago

AsterNighT commented 8 months ago

This is a first step towards #32

For now it only covers Settings.vue. And there are choices to make:

  1. The naming of the i18n keys, I'm not familiar with that. But I can see if not properly handled this could cause namespace conflicts.
  2. Apparently the locale setting should be stored in the settings. Will this break compatibility again?
  3. It seems we can, instead of having a separate file cn.json to store all the strings, put i18s string directly into SFC. (See https://vue-i18n.intlify.dev/guide/advanced/sfc.html) Which is the better practice?

If all there trouble can be resolved I suppose a i18n key can be returned from rust calls which maps to an explaination to the error.

Signed-off-by: AsterNighT asternight@outlook.com

AsterNighT commented 8 months ago

Also, to replace all occurrences of raw texts in the code is quite some labor. Help or advices on how to do that in a smart way is appreciated.

mcthesw commented 8 months ago

Nice work! In my opinion, put i18s into SFC is better. The locale messages are used as the local scope in SFC. So he probability of conflict is smaller. Besides , translators can intuitively know where the text will be placed.

The locale setting should be stored in the settings. Maybe there will be a new field in settings, but it won't break compatibility. We can give it a default value to avoid crashing during deserialization.

like this

pub enum Locale{
    cn,
    eng
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Settings {
    pub prompt_when_not_described: bool,
    pub extra_backup_when_apply: bool,
    #[serde(default = Locale::cn)]
    pub locale: Locale,
}
AsterNighT commented 8 months ago

Nice work! In my opinion, put i18s into SFC is better. The locale messages are used as the local scope in SFC. So he probability of conflict is smaller. Besides , translators can intuitively know where the text will be placed.

The locale setting should be stored in the settings. Maybe there will be a new field in settings, but it won't break compatibility. We can give it a default value to avoid crashing during deserialization.

like this

pub enum Locale{
    cn,
    eng
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Settings {
    pub prompt_when_not_described: bool,
    pub extra_backup_when_apply: bool,
    #[serde(default = Locale::cn)]
    pub locale: Locale,
}

LGTM, the SFC style i18n seems to require some extra settings in bundler, I'll look into it.

AsterNighT commented 8 months ago

Ok, this is a bit more complicated than I expected.

SFC works pretty well on the templates, but there are some texts outside of templates, as https://github.com/mcthesw/game-save-manager/blob/50abd0c71497fcc2171e380bf26b26033513a8dc/src/views/Home.vue#L64

If we use SFC i18n, the strings will be put in a local scope, as far as I learned there seems to be no good way to access a local i18n scope in a script block. Now in the code this is done via accessing the global scope, as is suggested here https://github.com/intlify/vue-i18n-next/issues/310.

I guess I'll fall back to the traditional locale files if there's no workaround.

mcthesw commented 8 months ago

Separate file is good enough, it's ok to use the traditional way

mcthesw commented 8 months ago

nice

mcthesw commented 8 months ago

I would like to add you to the thanks list. Can I use the GitHub username directly? You can also provide me a nickname

AsterNighT commented 8 months ago

I would like to add you to the thanks list. Can I use the GitHub username directly? You can also provide me a nickname

Sure thing. AsterNighT will do.